processFields call using PHP

  • 1.8K Views
  • Last Post 07 December 2012
GaryW posted this 07 December 2012

I can get one field to process using the sample PHP code and substituting $url = 'http://cloud.ocrsdk.com/processTextField?language=english&textType=handprinted&region=77,568,275,763';

However, when I try to use the processFields call, my code never completes. The first section is successful, as I get a return of taskID and status = submitted. Not sure if I'm passing the taskID to processFields call correctly.

<?php

// 1. Send image to Cloud OCR SDK using submitImage call // 2. Get response as xml // 3. Read taskId from xml // 4. Pass taskId to processFields OCR SDK and execute with XML file for field definitions // 5. Read taskID from xml // 6. loop until fields are processed // 7. download result

// Name of application you created $applicationId = 'myFormReader'; // Password should be sent to your e-mail after application was created $password = 'mypasswordxyz'; $fileName = '614PCB006.jpg'; $xmlName = 'fields.xml';

// Get path to file that we are going to recognize $local_directory=dirname(FILE).''; $filePath = $local_directory.'/'.$fileName; if(!file_exists($filePath)) { die('File '.$filePath.' not found.'); }

// Get path to file that we are going to use for the field definitions

$local_directory=dirname(FILE).''; $xmlPath = $local_directory.'/'.$xmlName; if(!file_exists($xmlPath)) { die('File '.$xmlPath.' not found.'); }

// set URL to use submitImage call

$url = 'http://cloud.ocrsdk.com/submitImage';

// Send HTTP POST request and ret xml response $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); $post_array = array( "my_file"=>"@".$filePath, ); curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $post_array); $response = curl_exec($curlHandle); if($response == FALSE) { $errorText = curl_error($curlHandle); curl_close($curlHandle); die($errorText); } curl_close($curlHandle);

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

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

// if I print taskID and status field here, I get what looks like a valid taskid // and status = submitted

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

// Send HTTP POST request and ret xml response $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_POST, 1); $post_array = array( "my_file"=>"@".$xmlPath, ); curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $post_array); $response = curl_exec($curlHandle); if($response == FALSE) { $errorText = curl_error($curlHandle); curl_close($curlHandle); die($errorText); } curl_close($curlHandle);

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

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

$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); $curlHandle = curl_init(); curl_setopt($curlHandle, CURLOPT_URL, $url.$qry_str); curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlHandle, CURLOPT_USERPWD, "$applicationId:$password"); $response = curl_exec($curlHandle); curl_close($curlHandle);

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

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

// Result is ready. Download it

$url = $arr["resultUrl"]; $curlHandle = curl_init(); curl_setopt($curlHandle, CURLOPT_URL, $url); curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1); // Warning! This is for easier out-of-the box usage of the sample only. // The URL to the result has https:// prefix, so SSL is required to // download from it. For whatever reason PHP runtime fails to perform // a request unless SSL certificate verification is off. curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($curlHandle); curl_close($curlHandle);

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

?> Here is the xml file, fields.xml: <document xmlns="http://ocrsdk.com/schema/taskDescription-1.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://ocrsdk.com/schema/taskDescription-1.0.xsd &lt;a href=" http:="" ocrsdk.com="" schema="" taskdescription-1.0.xsd"="">">http://ocrsdk.com/schema/taskDescription-1.0.xsd"> <page applyto="0,1,3,4"> <text id="Field1" left="77" top="568" right="275" bottom="763"> <language>English</language> <texttype>handprinted</texttype> <onetextline>true</onetextline> </text> </page> </document>

Order By: Standard | Newest | Votes
Dmitry Me posted this 07 December 2012

What is the last line being executed before it hangs?

GaryW posted this 07 December 2012

// if I print taskID and status field here, I get what looks like a valid taskid // and status = submitted

After the first section. I know it goes to at least here. When I've tried to set break points beyond here I get header errors

GaryW posted this 08 December 2012

problem solved. Changed <page applyto=""> parameter. My file is only single page

<page applyto="0"> just an FYI for anyone wanting to debug their code, write parameters to a log file such as: $myFile = "logfile.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "processFields url: ".$url.$qry_str."\n"; fwrite($fh, $stringData); fclose($fh);

Close