$applicationId = 'urvishaocr'; $password = ''; $fileName = 'input.png';

$local_directory=dirname(FILE).'/images/';

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

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERPWD, "$applicationId:$password"); curl_setopt($ch, CURLOPT_POST, 1); $post_array = array( "my_file"=>"@".$fileName, );

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array); $response = curl_exec($ch);

curl_close($ch); echo "

";
  print_r($response);
  die;

$xml = simplexml_load_string($response); $arr = $xml->task[0]->attributes();

$taskid = $arr["id"];

$url = 'http://cloud.ocrsdk.com/getTaskStatus'; $qry_str = "?taskid=$taskid";

do { sleep(5); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url.$qry_str); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERPWD, "$applicationId:$password"); $response = curl_exec($ch); curl_close($ch);

$xml = simplexml_load_string($response);
$arr = $xml->task[0]->attributes();

} while($arr["status"] != "Completed");

$url = $arr["resultUrl"]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch);

// Let user donwload rtf result header('Content-type: application/rtf'); header('Content-Disposition: attachment; filename="file.rtf"'); echo $response; ?>

i have put image in that directory only not in iamges folder. but i am not getting $response.Can you please help me...

asked 19 Jun '12, 14:35

urvisha's gravatar image

urvisha
1113

edited 19 Jun '12, 16:28

Please reset your application password ASAP and never publish it on public sites.

(19 Jun '12, 16:20) Dmitry Me ♦♦

The folder to take the file from is controlled by this line:

$local_directory=dirname(__FILE__).'/images/';

If you have your file in some folder other than /images subfolder of the folder where the sample is located you have to modify that line accordingly.

link

answered 19 Jun '12, 16:39

Dmitry%20Me's gravatar image

Dmitry Me ♦♦
2387

edited 19 Jun '12, 17:51

Hi thanks for your reply. I have put php file in ocr folder and put image in ocr folder itself .But when i run that page in $response i didnt get anything.

(19 Jun '12, 16:53) urvisha

@urvisha: Have you edited the code to account for such files relative location?

(19 Jun '12, 17:17) Dmitry Me ♦♦

I have changed in this code snippet only. $post_array = array( "my_file"=>"@".$fileName, ); Because my image file is located in the folder where php file is there.I ahve checked in php configuration curl is enable then also i am not getting any response in curl. Can you please help me out?

(19 Jun '12, 17:24) urvisha

@urvisha: This will not work - PHP runtime won't search for the file in the same directory as where the code file is unless you prepend it with dirname(__FILE__).

(19 Jun '12, 17:51) Dmitry Me ♦♦

@Dmitry Me : I have prepend $local_directory=dirname(FILE).'\images\'; this but i didt get any response in $response.Is there any prerequisite to setup for running this code? I am using appServer.Please let me know. Thanks.

(20 Jun '12, 08:48) urvisha

@urvisha: The sample should run "out of the box" once the correct credentials and path are provided. To ensure this is not a filepath problem could you please pull the latest changes from Github that we recently made to this sample?

(20 Jun '12, 13:06) Dmitry Me ♦♦

@Dmitry Me : Thanks for replying.I have used latest code from gitHub while testing it gives me file.rtf but there is no content in the file.Its blank.Can you please help me?

(21 Jun '12, 13:21) urvisha

@urvisha Turns out curl fails to download a file from URLs starting with https://, so for the purpose of trial use the way to go is to just disable SSL certificate verification as it's done in the latest change.

(21 Jun '12, 13:59) Dmitry Me ♦♦

@Dmitry Me: Thank you so much...Its working fine.. Thanks again..

(21 Jun '12, 14:24) urvisha
showing 5 of 9 show 4 more comments

Hi Dmitry,

I am not getting any response when I do curl_exec here:

$response = curl_exec($curlHandle);

The file path is fine, I am not getting any file error. I am just getting no response so the PHP Demo dies.

Any idea what I'm doing wrong?

$filePath = '/home/user1/public/OCR/sample.pdf'; if(!file_exists($filePath)) { die('File '.$filePath.' not found.'); } if(!is_readable($filePath) ) { die('Access to file '.$filePath.' denied.'); }

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

/ if (function_exists('curl_close')) { print "foo defined\n"; } else print "Function Doesn't Exist\n"; die; /

$curlHandle = curl_init(); curl_setopt($curlHandle, CURLOPT_URL, $url); 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); print_r2($response); if($response == FALSE) { $errorText = curl_error($curlHandle); curl_close($curlHandle);

die($errorText);

} $httpCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE); curl_close($curlHandle);

// Parse xml response $xml = simplexml_load_string($response);

if($httpCode != 200) { if(property_exists($xml, "message")) { die($xml->message); } die("unexpected response ".$response); }

$arr = $xml->task[0]->attributes(); $taskStatus = $arr["status"]; if($taskStatus != "Queued") { die("Unexpected task status ".$taskStatus); }

// Task id $taskid = $arr["id"];

// 4. Get task information in a loop until task processing finishes // 5. If response contains "Completed" staus - extract url with result // 6. Download recognition result (text) and display it

$url = 'http://cloud.ocrsdk.com/getTaskStatus'; $qry_str = "?taskid=$taskid";

// Check task status in a loop until it is finished // TODO: support states indicating error while(true) { sleep(5); $curlHandle = curl_init(); curl_setopt($curlHandle, CURLOPT_URL, $url.$qry_str); curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlHandle, CURLOPT_USERPWD, "$applicationId:$password"); curl_setopt($curlHandle, CURLOPT_USERAGENT, "PHP Cloud OCR SDK Sample"); $response = curl_exec($curlHandle); $httpCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE); curl_close($curlHandle);

// parse xml
$xml = simplexml_load_string($response);
if($httpCode != 200) {
  if(property_exists($xml, "message")) {
    die($xml->message);
  }
  die("Unexpected response ".$response);
}
link

answered 26 Apr '13, 20:28

enjayzroc's gravatar image

enjayzroc
111

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×59
×33

Asked: 19 Jun '12, 14:35

Seen: 3,382 times

Last updated: 31 May '13, 13:24

© 2016 ABBYY. All rights Reserved. www.ABBYY.com | Privacy Policy | Legal