Advertisement
Guest User

Untitled

a guest
Dec 14th, 2012
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.37 KB | None | 0 0
  1. <?php
  2.  
  3.   // 1. Send image to Cloud OCR SDK using processImage call
  4.   // 2. Get response as xml
  5.   // 3. Read taskId from xml
  6.  
  7.   // Name of application you created
  8.   $applicationId = 'Lbox LLC';
  9.   // Password should be sent to your e-mail after application was created
  10.   $password = '===password===';
  11.   $fileName = 'walmart_range_medium.jpg';
  12.  
  13.   // Get path to file that we are going to recognize
  14.   $local_directory=dirname(__FILE__).'/images';
  15.  
  16.   $filePath = $local_directory.'/'.$fileName;
  17.   if(!file_exists($filePath))
  18.   {
  19.     die('File '.$filePath.' not found.');
  20.   }
  21.  
  22.   // Recognizing with English language to rtf
  23.   // You can use combination of languages like ?language=english,russian or
  24.   // ?language=english,french,dutch
  25.   // For details, see API reference for processImage method
  26.   $url = 'http://cloud.ocrsdk.com/processImage?language=english&exportFormat=txt';
  27.   $filePath = '/var/www/images/walmart_range_medium.jpg';
  28.   $post_array = array(
  29.       "my_file"=>"@".$filePath,
  30.   );
  31.  
  32.   // Send HTTP POST request and ret xml response
  33.   $curlHandle = curl_init();
  34.   curl_setopt($curlHandle, CURLOPT_URL, $url);
  35.   curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
  36.   curl_setopt($curlHandle, CURLOPT_USERPWD, "$applicationId:$password");
  37.   curl_setopt($curlHandle, CURLOPT_POST, 1);
  38.   curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $post_array);
  39.  
  40.   $response = curl_exec($curlHandle);
  41.  
  42.   if($response == FALSE) {
  43.     $errorText = curl_error($curlHandle);
  44.     curl_close($curlHandle);
  45.     die($errorText);
  46.   }
  47.   curl_close($curlHandle);
  48.  
  49.  
  50.  
  51.  
  52.   echo "Send POST";
  53.  
  54.   // Parse xml response
  55.   $xml = simplexml_load_string($response);
  56.   $arr = $xml->task[0]->attributes();
  57.  
  58.   // Task id
  59.   $taskid = $arr["id"];  
  60.  
  61.   // 4. Get task information in a loop until task processing finishes
  62.   // 5. If response contains "Completed" staus - extract url with result
  63.   // 6. Download recognition result (text) and display it
  64.  
  65.   $url = 'http://cloud.ocrsdk.com/getTaskStatus';
  66.   $qry_str = "?taskid=$taskid";
  67.  
  68.   // Check task status in a loop until it is finished
  69.   // TODO: support states indicating error
  70.   do
  71.   {
  72.     sleep(5);
  73.     $curlHandle = curl_init();
  74.     curl_setopt($curlHandle, CURLOPT_URL, $url.$qry_str);
  75.     curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
  76.     curl_setopt($curlHandle, CURLOPT_USERPWD, "$applicationId:$password");
  77.     $response = curl_exec($curlHandle);
  78.     curl_close($curlHandle);
  79.  
  80.     // parse xml
  81.     $xml = simplexml_load_string($response);
  82.     $arr = $xml->task[0]->attributes();
  83.   }
  84.   while($arr["status"] != "Completed");
  85.  
  86.   // Result is ready. Download it
  87.  
  88.   $url = $arr["resultUrl"];  
  89.   $curlHandle = curl_init();
  90.   curl_setopt($curlHandle, CURLOPT_URL, $url);
  91.   curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
  92.   // Warning! This is for easier out-of-the box usage of the sample only.
  93.   // The URL to the result has https:// prefix, so SSL is required to
  94.   // download from it. For whatever reason PHP runtime fails to perform
  95.   // a request unless SSL certificate verification is off.
  96.   curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);
  97.   $response = curl_exec($curlHandle);
  98.   curl_close($curlHandle);
  99.  
  100.   // Let user donwload rtf result
  101.   header('Content-type: application/txt');
  102.   header('Content-Disposition: attachment; filename="$fileName.txt"');
  103.   echo $response;
  104. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement