我尝试使用cURL向网站发送POST,我希望以字符串格式接收请求
我的代码是:
<?php
$ch = curl_init();
//$post = array('text' => 'I am driving an ice cream truck');
$params = array('I am driving an ice cream truck');
$postData = 'text';
foreach($params as $k => $v)
{
$postData .= $k . '='.$v.'&';
}
rtrim($postData, '&');
curl_setopt($ch, CURLOPT_URL, 'http://beta.visl.sdu.dk/visl/en/parsing/automatic/parse.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_POST, 1);
//curl_exec($ch);
$data = curl_exec($ch);
echo $data;
?>
当我运行代码时,我没有得到请求的响应,只有没有任何POST的页面。
我做错了什么?
答案 0 :(得分:1)
您遗漏了网站所需的一些额外信息,例如paser和visualitzation,您也可以将数组发送到CURLOPT _ POSTFIELDS
所以,建立如下所需的参数:
$params = array(
'text' => 'I am driving an ice cream truck',
'paraser' => 'dep-eb',
'visual' => 'niceline'
);
然后发送如下数据:
curl_setopt($ch, CURLOPT_URL, 'http://beta.visl.sdu.dk/visl/en/parsing/automatic/parse.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
注意构建字符串参数的http_build_query()
函数。