nitin jain
nitin jain

Reputation: 298

php curl response blank randomly

We are implementing api with curl. We send xml request and get xml response. Some cases randomly we did not receive any response. When we co-ordinated with api provider they told there is no request hit on there server in case of blank response.

How we will know, its not hit on api provider server. Is there any header response ?

Upvotes: 0

Views: 715

Answers (4)

Misunderstood
Misunderstood

Reputation: 5665

Some troubleshooting cURL options:

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($ch, CURLOPT_TIMEOUT,100);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);

Get results

$data = curl_exec($ch);
if (curl_errno($ch)){
    $data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
$responseHeader = substr($data,0,$skip);
$info = curl_getinfo($ch);
$requestHeader = $info['request_header'];
$info = var_export($info,true);
echo "<pre>$requestHeader \n\n $responseHeader\n\n $info \n $data";

Upvotes: 1

r4ven
r4ven

Reputation: 303

Add to your curl query:

curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

curl will be more verbose

curl_getinfo($ch);

can also be helpfull

Upvotes: 0

Zgr3doo
Zgr3doo

Reputation: 1855

You need to add some code to handle response I don't know how your code looks like but it may be as simple as this

// add this line before curl_exec
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  

$response = curl_exec($curl);

$status = curl_getinfo($curl);
// do this before closing curl connection
curl_close($curl);  

and here is some more details about curl_getinfo http://php.net/manual/en/function.curl-getinfo.php

Upvotes: 0

Imtiaz Pabel
Imtiaz Pabel

Reputation: 5443

You can get curl error and errorno.Use this

var_dump(curl_errno($ch));
var_dump(curl_error($ch));

Upvotes: 1

Related Questions