gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 23:49:02 27 4
gpt4 key购买 nike

我正在尝试使用 http://nlp.stanford.edu:8080/corenlp/process 处的表单以编程方式处理我的声明.我在 PHP/CURL 中有以下代码片段。但是,它没有处理语句,而是返回表单的 HTML——就好像没有发送 post 参数一样。我检查了我是否正在发送所需的参数。有人可以指导我做错了什么吗?

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

最佳答案

您必须将 $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;

关于php - 使用 CURL 和 PHP 的 POST 请求无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15077433/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com