PHP Batch Client

From Textserver wiki
Revision as of 17:22, 20 January 2016 by Padro (Talk | contribs)

Jump to: navigation, search
 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 ?>