Difference between revisions of "PHP Batch Client"

From Textserver wiki
Jump to: navigation, search
 
Line 17: Line 17:
 
   // set query elements
 
   // set query elements
 
   echo 'Input ZIP file: '; $fname = rtrim(fgets(STDIN));
 
   echo 'Input ZIP file: '; $fname = rtrim(fgets(STDIN));
  echo 'Output ZIP file: '; $outfile = rtrim(fgets(STDIN));
 
 
   echo 'Language: '; $lang = rtrim(fgets(STDIN));
 
   echo 'Language: '; $lang = rtrim(fgets(STDIN));
 
   echo 'Output format (xml,json,conll,naf): '; $out = rtrim(fgets(STDIN));
 
   echo 'Output format (xml,json,conll,naf): '; $out = rtrim(fgets(STDIN));
Line 52: Line 51:
 
   $tkid = (string) new SimpleXMLElement($result);
 
   $tkid = (string) new SimpleXMLElement($result);
 
   print "Job submitted. Token id=".$tkid."\n";  
 
   print "Job submitted. Token id=".$tkid."\n";  
 
  // prepare request to poll for completion and retrieve results
 
  $h = curl_init();
 
  curl_setopt($h, CURLOPT_URL, $TextServer_URL."/resultRetrieve");
 
  curl_setopt($h, CURLOPT_POST, true);
 
  curl_setopt($h, CURLOPT_POSTFIELDS, array(
 
                                      'username' => $user,
 
                                      'tokenID' => $tkid,
 
                                      ) );
 
  curl_setopt($h, CURLOPT_HEADER, false);
 
  curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
 
 
  // Periodically poll server until batch job is ended.
 
  $nseconds=5;
 
  while (TRUE) {   
 
    print "Sleeping for ".$nseconds." seconds\n";
 
    sleep($nseconds);
 
 
    // execute request 
 
    print "Polling server for job completion...\n";
 
    $result = curl_exec($h);
 
 
    // check for completion
 
    $err = curl_getinfo($h, CURLINFO_HTTP_CODE);
 
    if ($err == 200) {
 
      // if response is ok, the job is done and we got the results. Exit loop.
 
      break;
 
    }
 
    else if ($err == 503 and strpos($result, '[TS-125]')!==FALSE) {
 
      // http status 503, textserver code TS-125 means the job is not finished yet. keep waiting
 
      print "Job not finished yet\n";
 
      continue;
 
    }
 
    else {
 
      // any other unexpected error happened. Abort.
 
      print "HTTP ".$err." - ".$result."\n";
 
      exit();
 
    }
 
  }
 
 
  curl_close($h);
 
 
  // write results to output ZIP file.
 
  $outf = fopen($outfile, "wb");
 
  fwrite($outf, $result);
 
  fclose($outf);
 
 
?>
 
?>
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 12:03, 21 January 2016

 1 #! /usr/bin/php5
 2 
 3 <?php
 4 
 5 //////////////////////////////////////////////////////////////////
 6 //
 7 //  Example client to submit an interactive request to TextServer 
 8 // SERVICENAME service. 
 9 //  Input may be a plain text or a text file.  No ZIP files.
10 //  Output will be in the requested format (XML, json, conll)
11 //
12 /////////////////////////////////////////////////////////////////
13   
14   $h = curl_init();
15   
16   // set query elements
17   echo 'Input ZIP file: '; $fname = rtrim(fgets(STDIN));
18   echo 'Language: '; $lang = rtrim(fgets(STDIN));
19   echo 'Output format (xml,json,conll,naf): '; $out = rtrim(fgets(STDIN));
20   echo 'TextServer Username: '; $user = rtrim(fgets(STDIN));
21   echo 'TextServer Password: '; $pwd = rtrim(fgets(STDIN));
22 
23   // build request
24   $TextServer_URL="http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws";
25   $service="SERVICENAME";
26   curl_setopt($h, CURLOPT_URL, $TextServer_URL."/processQuery/".$service); 
27   curl_setopt($h, CURLOPT_POST, true);
28   curl_setopt($h, CURLOPT_POSTFIELDS, array(
29                                        'username' => $user,
30                                        'password' => $pwd,
31                                        'file' => '@'.realpath($fname),
32                                        'language' => $lang,
33                                        'output' => $out,
34                                        'interactive' =>'0') );
35   curl_setopt($h, CURLOPT_HEADER, false);
36   curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
37 
38   // execute request
39   $result = curl_exec($h);
40 
41   // check for any error
42   $err = curl_getinfo($h, CURLINFO_HTTP_CODE);
43   if ($err != 200) {
44     print "HTTP ".$err." - ".$result."\n";
45     exit();
46   }
47   curl_close($h);
48 
49   // Server response should include a job tokenID, retrieve it
50   $tkid = (string) new SimpleXMLElement($result);
51   print "Job submitted. Token id=".$tkid."\n"; 
52 ?>