继上一篇介绍ue4打开web url窗口,这篇就来介绍下怎么访问php接口。
要做的两步:
1.c++自己写个接受请求的方法
f Post lhc-URL Request就是自定义的c++方法,
/**###lhc * 增加一个参数,接收一个FString,该参数的意义是:用URL的途径向php webservice发送数据,该数据的格式是key=value&key=value....** @param WorldContextObject * @param url **/void UJsonFieldData::PostRequest_lhc(UObject* WorldContextObject, const FString pm_lhcDataconst, const FString& url){ /*FString outStr; TSharedRef> JsonWriter = TJsonWriterFactory ::Create(&outStr); // Start writing the response WriteObject(JsonWriter, "", new FJsonValueObject(lhc_jsonData)); JsonWriter->Close(); // Log the post data for the user (OPTIONAL) UE_LOG(LogTemp, Warning, TEXT("Post data: %s"), *outStr);*/ // Create the post request with the generated data TSharedRef< IHttpRequest > HttpRequest = FHttpModule::Get().CreateRequest(); HttpRequest->SetVerb("POST"); HttpRequest->SetURL(CreateURL(url)); //###lhc 因为目前不知道php webservice取得JSON数据的代码怎么写,而且目前php service也没有必须获得客户端JSON数据(一个对象结构)的需求, //因此,暂时先通过URL的方式发送数据,用Key=value的形式,每组Key=Value之间用&分隔,如:name=a1&password=q1 //HttpRequest->SetHeader("Content-Type", "application/json"); HttpRequest->SetHeader("Content-Type", "application/x-www-form-urlencoded"); //###lhctmp //HttpRequest->SetContentAsString(outStr); //FString siwe = "name=a1&password=q1"; HttpRequest->SetContentAsString(pm_lhcDataconst); HttpRequest->OnProcessRequestComplete().BindUObject(this, &UJsonFieldData::OnReady); // Execute the request HttpRequest->ProcessRequest();}
2.按钮event