简体   繁体   English

使用CURL和PHP的POST请求无法正常工作

[英]POST Request using CURL and PHP not working properly

I am trying to programmatically process my statement using the form at http://nlp.stanford.edu:8080/corenlp/process . 我正在尝试使用http://nlp.stanford.edu:8080/corenlp/process上的表单以编程方式处理我的语句。 I have the following code snippet in PHP/CURL. 我在PHP / CURL中有以下代码片段。 However, instead of processing the statement, it is returning the HTML for the form - as if the post parameters are not being sent. 但是,它不是处理语句,而是返回表单的HTML - 就好像没有发送post参数一样。 I checked that I am sending the required paramaters. 我检查过我发送了所需的参数。 Can someone guide me as to 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;

You have to convert the $data array into a string... Also, Be sure to urlencode() the individual values. 您必须将$data数组转换为字符串...另外,请确保urlencode()各个值。

$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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM