POST request using CURL and PHP does not work properly

I am trying to programmatically process my expression using the form http://nlp.stanford.edu:8080/corenlp/process . I have the following code snippet in PHP / CURL. However, instead of processing the operator, it returns HTML for the form - as if the message parameters were not being sent. I checked that I am sending the necessary parameters. Can someone help me on what I am doing wrong?

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://nlp.stanford.edu:8080/corenlp/process"); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/14.0 (compatible; MSIE 6.0; Windows NT 5.1)"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_POST, true); $data = array( 'outputFormat' => 'xml', 'input' => 'Here is a statement to process', 'Process' => 'Submit Query' ); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $result = curl_exec($ch); echo $result; 
+4
source share
1 answer

You need to convert the $data array to a string ... Also, make sure urlencode() separate value.

 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://nlp.stanford.edu:8080/corenlp/process"); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/14.0 (compatible; MSIE 6.0; Windows NT 5.1)"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_POST, true); $data = array( "outputFormat" => "xml", "input" => "Here is a statement to process", "Process" => "Submit Query" ); $data_string = ""; foreach($data as $key=>$value){ /// YOU HAVE TO DO THIS $data_string .= $key.'='.urlencode($value).'&'; /// AND THIS } curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); $result = curl_exec($ch); echo $result; 
0
source

All Articles