2

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?

1
  • 1
    It appears that you're getting the headers in your output/response. You can turn that off CURLOPT_HEADER, 0 if you're not doing so already. Also try var_dump($response) - Commented Aug 5, 2015 at 17:57

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I realized after a closer look. Thx anyway.
I commented out curl_setopt($ch, CURLOPT_HEADER, 1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.