【问题标题】:POST Request using CURL and PHP not working properly使用 CURL 和 PHP 的 POST 请求无法正常工作
【发布时间】:2013-02-11 05:08:36
【问题描述】:

我正在尝试使用http://nlp.stanford.edu:8080/corenlp/process 的表单以编程方式处理我的声明。我在 PHP/CURL 中有以下代码 sn-p。但是,它不是处理语句,而是返回表单的 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;

【问题讨论】:

  • 您是否复制了该表单的所有内容? cookie、引用者、反垃圾邮件隐藏字段等...?
  • 提交按钮未标注Submit Query
  • 页面中有一些cookies,请尝试包含它们
  • 如何添加cookies、referers等?对不起,我是 CURL/PHP 的新手
  • 对于 cookie - curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt"); curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt"); 对于推荐人 curl_setopt($ch, CURLOPT_REFERER, "http://www.example.net");

标签: php post curl


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2021-04-10
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    相关资源
    最近更新 更多