Francesca
Francesca

Reputation: 28148

HTTP status code 0, how can I confirm a successful result? (cURL and PHP)

I'm using cURL and PHP to post data as JSON to an API.

The data posts correctly but I get a status code of 0.

I need a way to confirm that the POST was successful so I can feedback to the user that their data was entered correctly.

What could be causing the 0 response? The API documentation expects either 200 (successful) or 401 (unsuccessful). Here is the doc

Here is the code I am using:

// The request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$auth_url);
curl_setopt($ch,CURLOPT_POST, count($xml));
curl_setopt($ch,CURLOPT_POSTFIELDS, $xml_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$apiemail:$apipassword");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($xml_string))                                                                       
); 
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
$result = curl_exec ($ch);
curl_close ($ch);

echo 'The result is: ' . $result . '<br />';
echo 'The status code is: ' . $status_code . '<br />';

Upvotes: 1

Views: 956

Answers (1)

wira
wira

Reputation: 90

$result = curl_exec ($ch);    
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code after exec

how about trying to execute it first then retrieve the status code?

Upvotes: 1

Related Questions