我正试图通过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>";
他们的API声明,他们要求帖子是表单编码的数据,这里是情感API。
摘录
要分析某些文本的情感,请执行HTTPPOSThttp://text-processing.com/api/sentiment/表单编码的数据包含要分析的文本。您将返回一个具有两个属性的JSON对象响应:
要使用curl在PHP中执行此操作,必须传递值为application/x-www-form-urlencoded
的Content-Type
的标头。
演示
$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>";
我认为curl在您端没有启用。我试过你的代码,它运行正常。
尝试启用卷曲,然后尝试。
- 打开位于apache''bin中的php.ini
- 取消注释
;extension=php_curl.dll
- 重新启动Apache
希望这将有助于