PHP cURL请求没有给出任何输出

时间:2016-04-01 07:56:59

标签: php curl

我正在尝试通过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>";

2 个答案:

答案 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然后尝试。

  1. 打开位于apache \ bin中的php.ini。
  2. 取消注释;extension=php_curl.dll
  3. 重启Apache。
  4. 希望这会有所帮助

相关问题