gman_donster
gman_donster

Reputation: 331

Need assistance with getting API response code into variable

I have a pHP curl call trying to do and have gotten it working; and i Have it so the output of call is put into an array using "return transfer: true"; but when i do this, the response code of API (200, 300 400 etc.) is not in the array outputted; and so I am wondering how I can get that response code also dumped into a variable for use later... here is my code:

$data = array(
'username' => $username,
'password' => $password,
);



$data = http_build_query($data); // convert array to urlencoded string

$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://myapiaddress', //URL to the API
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $json,
CURLOPT_HEADER => true, 
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form- urlencoded','Accept: application/json',$token_string) 

));

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);

$resp = curl_exec($curl);
curl_close($curl); 

Thanks so much, Gerard

Upvotes: 0

Views: 29

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

Use curl_getinfo() with CURLINFO_HTTP_CODE on the resource handle:

$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

Upvotes: 1

Related Questions