我正在尝试通过PHP cURL执行此命令行curl命令,我在命令行中完美地获得了输出,但是当我尝试使用PHP时却不一样。
curl -d "text=terrible" http://text-processing.com/api/sentiment/
试图写一个PHP cURL,但我没有得到任何输出。
$data = "text=terrible";
$ch = curl_init('http://text-processing.com/api/sentiment/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
echo "<pre>";
print_r($response);
echo "</pre>";
答案 0 :(得分:1)
他们的API声明他们要求帖子在此处sentiment api形成编码数据。
要分析某些文字的情绪,请使用包含您要分析的文本的表单编码数据对http://text-processing.com/api/sentiment/执行HTTP POST。您将获得具有2个属性的JSON对象响应:
要在PHP中使用curl执行此操作,您必须传递Content-Type
的标头,其值为application/x-www-form-urlencoded
。
$data = "text=terrible";
$ch = curl_init('http://text-processing.com/api/sentiment/');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo "<pre>";
print_r($response);
echo "</pre>";
答案 1 :(得分:0)
我认为curl不会启用。我尝试了你的代码并且运行正常。
尝试启用curl然后尝试。
;extension=php_curl.dll
。希望这会有所帮助