# How to receive HTTP response from a php script?

**URL:** <https://forums.unrealengine.com/t/how-to-receive-http-response-from-a-php-script/299288>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [November 18, 2014, 7:20pm UTC](https://forums.unrealengine.com/t/how-to-receive-http-response-from-a-php-script/299288 "2014-11-18T19:20:29Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_c66b90a1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_c66b90a1](https://forums.unrealengine.com/u/anonymous_user_c66b90a1)\
**Post date:** [November 18, 2014, 7:20pm UTC](https://forums.unrealengine.com/t/how-to-receive-http-response-from-a-php-script/299288/1 "2014-11-18T19:20:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![abhishek17](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@abhishek17](https://forums.unrealengine.com/u/abhishek17)\
**Post date:** [November 18, 2014, 7:31pm UTC](https://forums.unrealengine.com/t/how-to-receive-http-response-from-a-php-script/299288/2 "2014-11-18T19:31:03Z")

</div>

I am struggling with this too. HELP!!!

---

<div class="post-metadata">

**Author:** ![SiddG](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SiddG](https://forums.unrealengine.com/u/SiddG)\
**Post date:** [November 20, 2014, 7:27pm UTC](https://forums.unrealengine.com/t/how-to-receive-http-response-from-a-php-script/299288/3 "2014-11-20T19:27:04Z")

</div>

Any help related to this would help a lot!

---

<div class="post-metadata">

**Author:** ![zompi2](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/zompi2/32/937524_2.png) [@zompi2](https://forums.unrealengine.com/u/zompi2)\
**Post date:** [November 21, 2014, 10:02am UTC](https://forums.unrealengine.com/t/how-to-receive-http-response-from-a-php-script/299288/4 "2014-11-21T10:02:14Z")

</div>

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](http://pl.wikipedia.org/wiki/Typ_MIME) (“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.

---

<div class="post-metadata">

**Author:** ![abhishek17](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@abhishek17](https://forums.unrealengine.com/u/abhishek17)\
**Post date:** [November 23, 2014, 1:30am UTC](https://forums.unrealengine.com/t/how-to-receive-http-response-from-a-php-script/299288/5 "2014-11-23T01:30:26Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![abhishek17](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@abhishek17](https://forums.unrealengine.com/u/abhishek17)\
**Post date:** [November 23, 2014, 3:57am UTC](https://forums.unrealengine.com/t/how-to-receive-http-response-from-a-php-script/299288/6 "2014-11-23T03:57:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zompi2](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/zompi2/32/937524_2.png) [@zompi2](https://forums.unrealengine.com/u/zompi2)\
**Post date:** [November 24, 2014, 8:10am UTC](https://forums.unrealengine.com/t/how-to-receive-http-response-from-a-php-script/299288/7 "2014-11-24T08:10:41Z")

</div>

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

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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_c66b90a1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_c66b90a1](https://forums.unrealengine.com/u/anonymous_user_c66b90a1)\
**Post date:** [November 25, 2014, 6:40pm UTC](https://forums.unrealengine.com/t/how-to-receive-http-response-from-a-php-script/299288/8 "2014-11-25T18:40:58Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![abhishek17](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@abhishek17](https://forums.unrealengine.com/u/abhishek17)\
**Post date:** [November 25, 2014, 7:22pm UTC](https://forums.unrealengine.com/t/how-to-receive-http-response-from-a-php-script/299288/9 "2014-11-25T19:22:03Z")

</div>

[http://docstore.mik.ua/orelly/webprog/php/ch07\_05.htm](http://docstore.mik.ua/orelly/webprog/php/ch07_05.htm)  
Try this.

---

<div class="post-metadata">

**Author:** ![JhonMax\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JhonMax\_1](https://forums.unrealengine.com/u/JhonMax_1)\
**Post date:** [February 11, 2015, 2:46am UTC](https://forums.unrealengine.com/t/how-to-receive-http-response-from-a-php-script/299288/10 "2015-02-11T02:46:46Z")

</div>

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)  
{

```
}

```

---

<div class="post-metadata">

**Author:** ![JhonMax\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JhonMax\_1](https://forums.unrealengine.com/u/JhonMax_1)\
**Post date:** [February 11, 2015, 2:48am UTC](https://forums.unrealengine.com/t/how-to-receive-http-response-from-a-php-script/299288/11 "2015-02-11T02:48:21Z")

</div>

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](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) {

}

---

<div class="post-metadata">

**Author:** ![abhishek17](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@abhishek17](https://forums.unrealengine.com/u/abhishek17)\
**Post date:** [February 12, 2015, 4:35pm UTC](https://forums.unrealengine.com/t/how-to-receive-http-response-from-a-php-script/299288/12 "2015-02-12T16:35:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TheGreatCabbage](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheGreatCabbage](https://forums.unrealengine.com/u/TheGreatCabbage)\
**Post date:** [April 22, 2017, 8:08pm UTC](https://forums.unrealengine.com/t/how-to-receive-http-response-from-a-php-script/299288/13 "2017-04-22T20:08:35Z")

</div>

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 🙂
