Difference between revisions of "PHP Batch Client"

From Textserver wiki
Jump to: navigation, search
(Created page with "<syntaxhighlight lang="php" line="1" > </syntaxhighlight>")
 
Line 1: Line 1:
 
<syntaxhighlight lang="php" line="1" >
 
<syntaxhighlight lang="php" line="1" >
 +
#! /usr/bin/php5
 +
 +
<?php
 +
 +
//////////////////////////////////////////////////////////////////
 +
//
 +
//  Example client to submit an interactive request to TextServer
 +
// SERVICENAME service.
 +
//  Input may be a plain text or a text file.  No ZIP files.
 +
//  Output will be in the requested format (XML, json, conll)
 +
//
 +
/////////////////////////////////////////////////////////////////
 +
 
 +
  $h = curl_init();
 +
 
 +
  // set query elements
 +
  echo 'Input ZIP file: '; $fname = rtrim(fgets(STDIN));
 +
  echo 'Output ZIP file: '; $outfile = rtrim(fgets(STDIN));
 +
  echo 'Language: '; $lang = rtrim(fgets(STDIN));
 +
  echo 'Output format (xml,json,conll,naf): '; $out = rtrim(fgets(STDIN));
 +
  echo 'TextServer Username: '; $user = rtrim(fgets(STDIN));
 +
  echo 'TextServer Password: '; $pwd = rtrim(fgets(STDIN));
 +
 +
  // build request
 +
  $TextServer_URL="http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws";
 +
  $service="SERVICENAME";
 +
  curl_setopt($h, CURLOPT_URL, $TextServer_URL."/processQuery/".$service);
 +
  curl_setopt($h, CURLOPT_POST, true);
 +
  curl_setopt($h, CURLOPT_POSTFIELDS, array(
 +
                                      'username' => $user,
 +
                                      'password' => $pwd,
 +
                                      'file' => '@'.realpath($fname),
 +
                                      'language' => $lang,
 +
                                      'output' => $out,
 +
                                      'interactive' =>'0') );
 +
  curl_setopt($h, CURLOPT_HEADER, false);
 +
  curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
 +
 +
  // execute request
 +
  $result = curl_exec($h);
 +
 +
  // check for any error
 +
  $err = curl_getinfo($h, CURLINFO_HTTP_CODE);
 +
  if ($err != 200) {
 +
    print "HTTP ".$err." - ".$result."\n";
 +
    exit();
 +
  }
 +
  curl_close($h);
 +
 +
  // Server response should include a job tokenID, retrieve it
 +
  $tkid = (string) new SimpleXMLElement($result);
 +
  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>

Revision as of 17:22, 20 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 'Output ZIP file: '; $outfile = rtrim(fgets(STDIN));
19   echo 'Language: '; $lang = rtrim(fgets(STDIN));
20   echo 'Output format (xml,json,conll,naf): '; $out = rtrim(fgets(STDIN));
21   echo 'TextServer Username: '; $user = rtrim(fgets(STDIN));
22   echo 'TextServer Password: '; $pwd = rtrim(fgets(STDIN));
23 
24   // build request
25   $TextServer_URL="http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws";
26   $service="SERVICENAME";
27   curl_setopt($h, CURLOPT_URL, $TextServer_URL."/processQuery/".$service); 
28   curl_setopt($h, CURLOPT_POST, true);
29   curl_setopt($h, CURLOPT_POSTFIELDS, array(
30                                        'username' => $user,
31                                        'password' => $pwd,
32                                        'file' => '@'.realpath($fname),
33                                        'language' => $lang,
34                                        'output' => $out,
35                                        'interactive' =>'0') );
36   curl_setopt($h, CURLOPT_HEADER, false);
37   curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
38 
39   // execute request
40   $result = curl_exec($h);
41 
42   // check for any error
43   $err = curl_getinfo($h, CURLINFO_HTTP_CODE);
44   if ($err != 200) {
45     print "HTTP ".$err." - ".$result."\n";
46     exit();
47   }
48   curl_close($h);
49 
50   // Server response should include a job tokenID, retrieve it
51   $tkid = (string) new SimpleXMLElement($result);
52   print "Job submitted. Token id=".$tkid."\n"; 
53 
54   // prepare request to poll for completion and retrieve results
55   $h = curl_init();
56   curl_setopt($h, CURLOPT_URL, $TextServer_URL."/resultRetrieve"); 
57   curl_setopt($h, CURLOPT_POST, true);
58   curl_setopt($h, CURLOPT_POSTFIELDS, array(
59                                        'username' => $user,
60                                        'tokenID' => $tkid,
61                                       ) );
62   curl_setopt($h, CURLOPT_HEADER, false);
63   curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
64 
65   // Periodically poll server until batch job is ended.
66   $nseconds=5;
67   while (TRUE) {    
68     print "Sleeping for ".$nseconds." seconds\n";
69     sleep($nseconds);
70 
71     // execute request   
72     print "Polling server for job completion...\n";
73     $result = curl_exec($h);
74 
75     // check for completion 
76     $err = curl_getinfo($h, CURLINFO_HTTP_CODE);
77     if ($err == 200) {
78       // if response is ok, the job is done and we got the results. Exit loop.
79       break;
80     }
81     else if ($err == 503 and strpos($result, '[TS-125]')!==FALSE) {
82       // http status 503, textserver code TS-125 means the job is not finished yet. keep waiting
83       print "Job not finished yet\n";
84       continue;
85     }
86     else {
87       // any other unexpected error happened. Abort.
88       print "HTTP ".$err." - ".$result."\n";
89       exit();
90     }
91   }
92 
93   curl_close($h);
94 
95   // write results to output ZIP file.
96   $outf = fopen($outfile, "wb");
97   fwrite($outf, $result);
98   fclose($outf);
99 ?>