OCR SDK PHP sample not working Answered

I really would like to use ABBYY OCR, but I can't get it to work! I when ever I run the code below on my Apache server, running PHP 5, I get this:

Fatal error: Call to a member function attributes() on a non-object in /home/content/76/7290476/html/dumbocr/abbyy_php_example.php on line 36

This is my PHP code:

// 1. Send image to Cloud OCR SDK using processImage call // 2. Get response as xml // 3. Read taskId from xml

// !!!!!!!!!! Enter your data here !!!!!!!!!! $applicationId = 'DumbDrive'; $password = '===password-here===='; $fileName = 'dselogo3.png';

// Get path to file that we are going to recognize $local_directory=dirname(FILE).'';

// Recognizing with English language to rtf // You can use combination of languages like ?language=english,russian or // ?language=english,french,dutch // For details, see API reference for processImage method $url = 'http://cloud.ocrsdk.com/processImage';

// Send HTTP POST request and ret xml response $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"=>"@".$local_directory.$fileName, ); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array); $response = curl_exec($ch); curl_close($ch);

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

// 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 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);

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

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

// Result is ready. Download it

$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;

Was this article helpful?

0 out of 0 found this helpful

Comments

5 comments

  • Avatar
    Nikolay_Kh

    According to the error you receive, you have something wrong with $arr variable. Try the following:

    1. Check the login credentials you use (they were sent to your email). I've checked sdk server log for your application, there were no HTTP calls from you.

    2. echo out the $response var and print_r the $xml object above. What are their values?

    3. Try running echo function_exists(simplexml_load_string); on your server to make sure you can parse xml the way it is proposed in the example.

    4. Check that the dselogo3.png is in the same directory with the abbyy_php_example.php file.

    If this won't help, please send the code you use (within a php file, so the code structure would be preserved) to cloudocrsdkbeta@abbyy.com

    Please let me know if you could work it out!

    0
  • Avatar
    Nikolay_Kh

    It seems that there was a minor error in the PHP code sample: path to the image file being sent to OCR Engine was stated incorrectly. You'll need to add a forward slash between the directory and the filename variables on line 28:

    "my_file"=>"@".$local_directory.'/'.$fileName,
    

    I've sent the working code to your email. We'll update the codesamples in our documentation shortly. Please let me know if you have any additional questions.

    1
  • Avatar
    swiftwolff

    Hi Nikolay,

    I'm having the same prb as Andrey above. I double check the above items and here are the results:

    1. I should have valid login credentials (I will email my code to you)
    2. I echo and print_r both $response and $xml, they do not show any value.
    3. function simplexml_load_string is valid on my server
    4. the image is the same directory with my script file.
    0
  • Avatar
    Nikolay_Kh

    Did you check a forward slash between the directory and the filename variables mentioned in the accepted answer?

    0
  • Avatar
    Nikolay_Kh

    Please check your $local_directory variable. In the code you provided it has an /images/ subfolder, so you have to either create this folder and place an image in it or remove it from the variable. Note that in this question there's no such folder in the image path.

    0

Please sign in to leave a comment.