Ileno Ileno
Ileno Ileno

Reputation: 23

json_decode PHP cURL post to API .NET

From PHP I post to an API on .NET (Win Server), using cURL

My last part of code is:

$response = curl_exec($ch);
$responseArray = json_decode($response, true);

When I do:

print_r($response);

I get in the browser:

HTTP/1.1 201 Created Cache-Control: no-cache Pragma: no-cache Content-Length: 64 Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Wed, 05 Aug 2015 12:44:50 GMT {"resultCode":0,"resultMessage":"Success","order_number":123456}

When I do:

print_r($responseArray);

I get blank screen.

So my question is how do I grab the variables from this response?

Upvotes: 2

Views: 583

Answers (1)

Adam T
Adam T

Reputation: 675

I would try setting the curlopt to not return Output Headers.

PHP: curl_setopt

The item you would set is CURLOPT_HEADER Example:

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false); 

At that point, your response ($response) should be the JSON-encoded string. For added bonus you may also want to enforce JSON output expectation if you're not currently using that setting.

// Set The Response Format to Json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));

More info here: PHP Curl, Request data return in application/json

Upvotes: 1

Related Questions