Difference between revisions of "PHP Interactive Client"

From Textserver wiki
Jump to: navigation, search
(Created page with "<syntaxhighlight lang="python" line="1" > </syntaxhighlight>")
 
Line 1: Line 1:
<syntaxhighlight lang="python" 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 'Text to analyze: '; $text = 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
 +
  # $service="SERVICENAME";
 +
  $service="tagger";
 +
  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_POSTFIELDS, array(
 +
                                      'username' => $user,
 +
                                      'password' => $pwd,
 +
                                      'text_input' => $text,
 +
                                      'language' => $lang,
 +
                                      'output' => $out,
 +
                                      'interactive' =>'1') );
 +
  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);
 +
 
 +
  # process results as needed;
 +
  print $result."\n";
 +
 
 +
?>
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 13:02, 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   $service="tagger";
26   curl_setopt($h, CURLOPT_URL, "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws/processQuery/".$service); 
27   curl_setopt($h, CURLOPT_POST, true);
28   curl_setopt($h, CURLOPT_POSTFIELDS, array(
29                                        'username' => $user,
30                                        'password' => $pwd,
31                                        'text_input' => $text,
32                                        'language' => $lang,
33                                        'output' => $out,
34                                        'interactive' =>'1') );
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   # process results as needed;
50   print $result."\n";
51 
52 ?>