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 having a similar issue. HELP!!!

If you want to use the delegate way (you’re currently using), make sure your class implement the delegate

@interface MyViewController : UIViewController <NSURLConnectionDataDelegate>
and you can use following delegate method to get the data

  • (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response
    {
    NSLog(@“Did Receive Response %@”, response);
    }
  • (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
    {
    NSLog(@“Did Receive Data %@”, data);
    }

Another example is using the sendAsynchronousRequest method, simply replace your registrationConnection

[NSURLConnection sendAsynchronousRequest:registrationRequest queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse* response, NSData* data, NSError* connectionError) {
NSLog(@“Response %@”, response);
NSLog(@“Data %@”, data); //Add a breakpoint here to find out whats the
}];