Jash Jacob
Jash Jacob

Reputation: 580

PHP cURL request is not giving any output

I'm trying to do this command line curl command through PHP cURL, I'm getting the output perfectly in commandline but not the same when I try with PHP.

curl -d "text=terrible" http://text-processing.com/api/sentiment/

Tried to write a PHP cURL but I didn't get any output.

    $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>";

Upvotes: 1

Views: 1651

Answers (2)

Kraang Prime
Kraang Prime

Reputation: 10509

Their API states they require the post to be form encoded data here sentiment api.

Excerpt

To analyze the sentiment of some text, do an HTTP POST to http://text-processing.com/api/sentiment/ with form encoded data containg the text you want to analyze. You’ll get back a JSON object response with 2 attributes:

To do this in PHP with curl, you must pass the header for Content-Type with the value application/x-www-form-urlencoded.

Demonstration

$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>";

Upvotes: 1

Rahul Vyas
Rahul Vyas

Reputation: 221

I think curl is not enable on your end. I tried your code and it is running properly.

Try to enable curl then try it.

  1. Open your php.ini, situated in apache\bin.
  2. Uncomment ;extension=php_curl.dll.
  3. Restart Apache.

Hope this will help

Upvotes: 0

Related Questions