1

I am trying to get the response code from the response header using cURL PHP.

When I send the request, this is the response header that is returned by MYOB AccountRight API:

HTTP/1.1 200 OK
Access-Control-Expose-Headers: Request-Context
Cache-Control: must-revalidate, private
Content-Encoding: gzip
Content-Type: application/json;charset=utf-8
Date: Thu, 20 May 2021 01:07:56 GMT
ETag: "XXXXXXXXX"
Expires: -1
Request-Context: appId=cid-v1:a4936349-ef26-4f8a-9268-XXXXXXXXX
Server: Microsoft-IIS/10.0
Vary: Accept-Encoding
X-AspNet-Version: 4.0.30319
X-Mashery-Message-ID: 2fc6b494-54e8-43e2-8bc4-XXXXXXXXX
X-Mashery-Responder: prod-j-worker-ap-southeast-2b-33.mashery.com
x-myobapi-elapsed: 1370
x-myobapi-requestid: bb0764c8-f62d-4848-bcae-XXXXXXXXX
X-Powered-By: ASP.NET
Content-Length: 1205
Connection: keep-alive

I have tried the solution from Getting HTTP code in PHP using curl , but I will not get the http code.

This is my code to get the accounts data:

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://ar1.api.myob.com/accountright/766d620e-a5eb-41c3-8343-XXXXXXXX/GeneralLedger/Account?$filter=Name%20eq%20\'Inventory\'%20or%20Name%20eq%20\'Cost%20Of%20Sales\'%20or%20Name%20eq%20\'Inventory%20Income\'',
  CURLOPT_HEADER => true,
  CURLOPT_NOBODY => true,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'x-myobapi-version: v2',
    'Accept-Encoding: gzip,deflate',
    'x-myobapi-key: '.$theAPIKey,
    'x-myobapi-cftoken: '.$theCFToken,
    'Authorization: Bearer '.$theAccessToken
  )
));

$response = curl_exec($curl);
$theInfo = curl_getinfo($response);
$http_code = $theInfo['http_code'];
curl_close($curl);
echo 'http code: ' . $http_code . '<br />';
echo '<pre>';
echo $response;
echo '</pre>';

When I echo the http code, nothing will be printed.

2
  • 1
    print_r($theInfo) and see what it contains. Commented May 20, 2021 at 1:30
  • Hi, nothing will be printed. Commented May 20, 2021 at 1:35

1 Answer 1

1

I think you need to pass $curl to the curl_getinfo method, not the $response

$response = curl_exec($curl);
$theInfo = curl_getinfo($curl);
$http_code = $theInfo['http_code'];

You can see the doco here.. https://www.php.net/manual/en/function.curl-getinfo.php

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

2 Comments

Hi, thank you for the reply. I can retrieve the http code now.
@nkm You should adjust your error reporting settings; that mistake should've generated PHP Warning: curl_getinfo() expects parameter 1 to be resource, bool given.

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.