Invalid Task ID

 Hello ,I tried using the sample code provided in the php documentation ,but i keep getting "Invalid task Id used when preparing getTastStatus Request"

I have looked every where on the code ,it seems to be correct .Kindly point me where the code is wrong .

Here is my code,i already set my application ID and Password 

...Line 78

  $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 = $serviceUrl.'/getTaskStatus';

  // Note: a logical error in more complex surrounding code can cause

  // a situation where the code below tries to prepare for getTaskStatus request

  // while not having a valid task id. Such request would fail anyway.

  // It's highly recommended that you have an explicit task id validity check

  // right before preparing a getTaskStatus request.

  if(empty($taskId)) {

    die("Invalid task id used when preparing getTaskStatus request");


  $qry_str = "?taskid=$taskid";


  // Check task status in a loop until it is finished


  // Note: it's recommended that your application waits

  // at least 2 seconds before making the first getTaskStatus request

  // and also between such requests for the same task.

  // Making requests more often will not improve your application performance.

  // Note: if your application queues several files and waits for them

  // it's recommended that you use listFinishedTasks instead (which is described

  // at




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

    curl_setopt($curlHandle, CURLOPT_FAILONERROR, true);

    $response = curl_exec($curlHandle);

    $httpCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);



    // parse xml

    $xml = simplexml_load_string($response);

    if($httpCode != 200) {

      if(property_exists($xml, "message")) {



      die("Unexpected response ".$response);


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

    $taskStatus = $arr["status"];

    if($taskStatus == "Queued" || $taskStatus == "InProgress") {

      // continue waiting



    if($taskStatus == "Completed") {

      // exit this loop and proceed to handling the result



    if($taskStatus == "ProcessingFailed") {

      die("Task processing failed: ".$arr["error"]);


    die("Unexpected task status ".$taskStatus);



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



  // Let user donwload rtf result

  header('Content-type: application/rtf');

  header('Content-Disposition: attachment; filename="file.rtf"');

  echo $responsse;




  • Avatar
    Oksana Serdyuk

    Please try to change the line 90 as it is done in our sample:

    if(empty($taskId) || (strpos($taskId, "00000000-0") !== false)) {


    When there is not enough credits on the application balance and the service returns the corresponding response with the NotEnoughCredits status, it looks in the following way:


        <task id="00000000-0000-0000-0000-000000000000" registrationTime="YYYY-MM-DDTHH:MM:SSZ" statusChangeTime="YYYY-MM-DDTHH:MM:SSZ" status="NotEnoughCredits" filesCount="0" credits="0" />


    Thus, we need also correctly handle such situation.

    Comment actions Permalink
  • Avatar

    Thanks ,but i have changed it ,its still the same error i get : "Invalid task id used when preparing getTaskStatus request "

    Comment actions Permalink
  • Avatar
    Oksana Serdyuk

    Could you please send us to the logs from Fiddler debugger (you can download the Fiddler debugger for free here: It allows seeing what requests do you send to the server. Also you can use it for debugging your code during developing the application in future.

    Comment actions Permalink

Please sign in to leave a comment.