Difference between revisions of "PHP Interactive Client"

From Textserver wiki
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
Line 4: Line 4:
 
<?php
 
<?php
  
########################################################################
+
///////////////////////////////////////////////////////////////////
#
+
//
# Example client to submit an interactive request to TextServer  
+
// Example client to submit an interactive request to TextServer  
# SERVICENAME service.  
+
// SERVICENAME service.  
# Input may be a plain text or a text file.  No ZIP files.
+
// Input may be a plain text or a text file.  No ZIP files.
# Output will be in the requested format (XML, json, conll)
+
// Output will be in the requested format (XML, json, conll)
#
+
//
########################################################################
+
///////////////////////////////////////////////////////////////////
  
 
   $h = curl_init();
 
   $h = curl_init();
 
    
 
    
   # set query elements
+
   // set query elements
 
   echo 'Text to analyze: '; $text = rtrim(fgets(STDIN));
 
   echo 'Text to analyze: '; $text = rtrim(fgets(STDIN));
 
   echo 'Language: '; $lang = rtrim(fgets(STDIN));
 
   echo 'Language: '; $lang = rtrim(fgets(STDIN));
Line 22: Line 22:
 
   echo 'TextServer Password: '; $pwd = rtrim(fgets(STDIN));
 
   echo 'TextServer Password: '; $pwd = rtrim(fgets(STDIN));
  
   # build request
+
   // build request
   # $service="SERVICENAME";
+
   $service="SERVICENAME";
  $service="tagger";
+
 
   curl_setopt($h, CURLOPT_URL, "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws/processQuery/".$service);  
 
   curl_setopt($h, CURLOPT_URL, "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws/processQuery/".$service);  
 
   curl_setopt($h, CURLOPT_POST, true);
 
   curl_setopt($h, CURLOPT_POST, true);
Line 37: Line 36:
 
   curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
 
   curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
  
   # execute request
+
   // execute request
 
   $result = curl_exec($h);
 
   $result = curl_exec($h);
  
   # check for any error
+
   // check for any error
 
   $err = curl_getinfo($h, CURLINFO_HTTP_CODE);
 
   $err = curl_getinfo($h, CURLINFO_HTTP_CODE);
 
   if ($err != 200) {
 
   if ($err != 200) {
Line 48: Line 47:
 
   curl_close($h);
 
   curl_close($h);
  
   # process results as needed;
+
   // process results as needed;
 
   print $result."\n";
 
   print $result."\n";
  
 
?>
 
?>
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 17:24, 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 'Text to analyze: '; $text = 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   $service="SERVICENAME";
25   curl_setopt($h, CURLOPT_URL, "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws/processQuery/".$service); 
26   curl_setopt($h, CURLOPT_POST, true);
27   curl_setopt($h, CURLOPT_POSTFIELDS, array(
28                                        'username' => $user,
29                                        'password' => $pwd,
30                                        'text_input' => $text,
31                                        'language' => $lang,
32                                        'output' => $out,
33                                        'interactive' =>'1') );
34   curl_setopt($h, CURLOPT_HEADER, false);
35   curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
36 
37   // execute request
38   $result = curl_exec($h);
39 
40   // check for any error
41   $err = curl_getinfo($h, CURLINFO_HTTP_CODE);
42   if ($err != 200) {
43     print "HTTP ".$err." - ".$result."\n";
44     exit();
45   }
46   curl_close($h);
47 
48   // process results as needed;
49   print $result."\n";
50 
51 ?>