response is empty

  • 895 Views
  • Last Post 24 May 2013
mynigo posted this 23 May 2013

I use sample code for PHP how ever after upload image to server. Anyone can help?

$url = 'http://cloud.ocrsdk.com/processImage?language=english&exportFormat=rtf';

// Send HTTP POST request and ret xml response
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $url);
curl_setopt ($curlHandle, CURLOPT_CONNECTTIMEOUT, 20 );
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlHandle, CURLOPT_USERPWD, "$applicationId:$password");
curl_setopt($curlHandle, CURLOPT_POST, 1);
curl_setopt($curlHandle, CURLOPT_USERAGENT, "PHP Cloud OCR SDK Sample");
$post_array = array(
    "my_file"=>"@".$filePath,
);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $post_array); 
$response = curl_exec($curlHandle);

$response is empty.

Dmitry Me posted this 24 May 2013

This code snippet is from PHP sample on Github:

$response = curl_exec($curlHandle);
if($response == FALSE) {
  $errorText = curl_error($curlHandle);
  curl_close($curlHandle);
  die($errorText);
}

Note the if($response == FALSE) { check - in case of error $response will be set to FALSE and if you echo it you indeed see empty output.

So the bottom line is you have to handle this case in a manner similar to how it is handled in the sample - anticipate FALSE and call curl_error() to obtain error details.

Close