How to receive HTTP response from a php script?

Hi. I have setup my project so that it sends post data to a website (php script). This is working fine. But, I want to receive a string back from the website. In my php, I am doing echo of the string I want to send back. In my project, this is my setup:

bool AHTTPHandlerActor::TransmitRequest()

{

FSagRequest CurrentRequest;

FString Dest;
Dest.Equals("XYZ.php");
CurrentRequest.SetDestination(Dest);

TSharedRef<IHttpRequest> Request = FHttpModule::Get().CreateRequest();

Request->SetHeader("Content-Type", "application/x-www-form-urlencoded");
Request->SetURL(TEXT("http://ABC.com/XYZ.php"));
Request->SetVerb(TEXT("POST"));



FString NumberOfLevelsString(TEXT("NumberOfLevels"));
FString NumberOfLevelsStringValue = FString::FromInt(GlobalArrayLength);

CurrentRequest.AddURLPair(NumberOfLevelsString, NumberOfLevelsStringValue); //This internally makes the              //                                              //string to send. This works, because I am successfully sending data to the website.  




Request->SetContentAsString(CurrentRequest.TheData);

Request->OnProcessRequestComplete().BindUObject(this, &AHTTPHandlerActor::OnResponseReceived);




if (!Request->ProcessRequest())
{
	
	return false;
}

delete LevelData;
return true;

}

Here is the responseReceived function:

 void AHTTPHandlerActor::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response,          bool bWasSuccessful)
             {
FString MessageBody = "";

// If HTTP fails client-side, this will still be called but with a NULL shared pointer!
if (!Response.IsValid())
{
	MessageBody = "{\"success\":\"Error: Unable to process HTTP Request!\"}";
}
else if (EHttpResponseCodes::IsOk(Response->GetResponseCode()))
{
	MessageBody = Response->GetContentAsString();

}
else
{
	MessageBody = FString::Printf(TEXT("{\"success\":\"HTTP Error: %d\"}"), Response->GetResponseCode());
}
ResponseStr = MessageBody;
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, ResponseStr);
//Closed(MessageBody);
    }

I am getting the response string as NULL. Does anyone know the issue?

I am struggling with this too. HELP!!!

Any help related to this would help a lot!

Hi.

First - the question: What is CurrentRequest.AddURLPair() ? Is it your method? Is it doing something with http request?

Then - check if CurrentRequest.TheData has the data when you’re passing it to the SetContentAsString.

Also - try to change Content-Type that fits best your needs. Here is a list of possible Content-Types Typ MIME – Wikipedia, wolna encyklopedia (“text/plain” should be fine).

The final thing - check what packages you receive. Use Wireshark to check if there is a payload in response.

Also always check bWasSuccessful first.

The code looks fine to me, maybe there is a problem on the server side?

Oh and one more thing: Make sure that the server is responding using UTF8 and is zero terminated.

Thanks so much for replying back. AddURLPair basically allows you to add var and its value to the stream to send. So, I am receiving the data correctly on the website, exactly the way I want to. Now, to send a responce, all I am doing in the php script is echo “xyz”. I thought that was enough to send back a response from a php script. Is there something I am doing wrong at the php side?

Actually, the echo works. I was not receiving any response because I was calling the transmit on game exit. So, the getResponce function is running. Thanks guys.

I can see the problem is solved, but just as a reminder for future generations :wink:

  1. Always use UTF8 encoding. Remember to save all php files using UTF8 and set GET/SET to UTF8 (see mb_http_output function)
  2. Always use proper Content-Types - “text/plain” for plain text, “application/json” for jsons etc.
  3. Always check bWasSuccessful and validate the response.

Thanks for replying. I am new to php programming. I know that php sends the html page as response. Do you know a way I can send a plain text as echo?

http://docstore.mik.ua/orelly/webprog/php/ch07_05.htm
Try this.

HELLO,i try to use httprequest,but the php website receive nothing ,can you help me,please?
#include “hzl.h”
#include “Myphpconnection.h”

AMyphpconnection::AMyphpconnection(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
FString PostData = FString(TEXT(“{"Tname":”)) + FString(TEXT(“"4446545646544"”)) + TEXT(“}”);

TSharedRef<IHttpRequest> HttpRequest = FHttpModule::Get().CreateRequest();
//HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/x-www-form-urlencoded"));
HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
	HttpRequest->SetVerb("POST");
HttpRequest->SetURL(TEXT("http://localhost/index.php"));

HttpRequest->SetContentAsString(PostData);
HttpRequest->OnProcessRequestComplete().BindUObject(this, &AMyphpconnection::OnUpdateRequestComplete);

// HttpRequest->ProcessRequest();

if (!HttpRequest->ProcessRequest())
{
	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(-1, 500.f, FColor::Yellow, FString::Printf(TEXT("%s"), *PostData));
		GEngine->AddOnScreenDebugMessage(1, 600.0f, FColor::Green, TEXT("Request failed at start!"));
	}
}
else
{
	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(-1, 500.f, FColor::Yellow, FString::Printf(TEXT("%s"), *PostData));
		GEngine->AddOnScreenDebugMessage(1, 600.0f, FColor::Green, TEXT("Request successd at start!"));
	}
}/**/

}

void AMyphpconnection::OnUpdateRequestComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{

}

HELLO,i try to use httprequest,but the php website receive nothing ,can you help me,please?

#include “hzl.h”
#include “Myphpconnection.h”

AMyphpconnection::AMyphpconnection(const class FPostConstructInitializeProperties& PCIP) : Super(PCIP) { FString PostData = FString(TEXT(“{"Tname":”)) + FString(TEXT(“"4446545646544"”)) + TEXT(“}”);

TSharedRef HttpRequest = FHttpModule::Get().CreateRequest();
//HttpRequest->SetHeader(TEXT(“Content-Type”), TEXT(“application/x-www-form-urlencoded”));
HttpRequest->SetHeader(TEXT(“Content-Type”), TEXT(“application/json”));
HttpRequest->SetVerb(“POST”);
HttpRequest->SetURL(TEXT(“http://localhost/index.php”));

HttpRequest->SetContentAsString(PostData);
HttpRequest->OnProcessRequestComplete().BindUObject(this, &AMyphpconnection::OnUpdateRequestComplete);
// HttpRequest->ProcessRequest();

if (!HttpRequest->ProcessRequest())
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 500.f, FColor::Yellow, FString::Printf(TEXT(“%s”), *PostData));
GEngine->AddOnScreenDebugMessage(1, 600.0f, FColor::Green, TEXT(“Request failed at start!”));
}
}
else
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 500.f, FColor::Yellow, FString::Printf(TEXT(“%s”), *PostData));
GEngine->AddOnScreenDebugMessage(1, 600.0f, FColor::Green, TEXT(“Request successd at start!”));
}
}/**/

}

void AMyphpconnection::OnUpdateRequestComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful) {

}

look at the code above. I can confirm that it works perfectly. If you have any specific questions, shoot at me

I spent hours trying to figure out why my posting wasn’t working, but I changed to UTF-8 and suddenly it worked! I don’t know why, but it was on UCS-2… Thanks :slight_smile: