ericbae
ericbae

Reputation: 9644

cURL question : how do I run this in PHP?

I want to access an API provided by Lymbix sentiment via PHP. The cURL command given is

curl -H "AUTHENTICATION:MY_API_KEY" \
-H "ACCEPT:application/json" \
-H "VERSION:2.1" \
http://gyrus.lymbix.com/tonalize \
-d "article=This is a sample sentence, does it make you happy? \
&return_fields=[]"

How would I run the above in PHP?

Thank you.

Upvotes: 0

Views: 169

Answers (2)

Luke Dennis
Luke Dennis

Reputation: 14550

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://gyrus.lymbix.com/tonalize" );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt($curl, CURLOPT_POST, true );   
curl_setopt($curl, CURLOPT_POSTFIELDS, "article=This is a sample sentence, does it make you happy?&return_fields=[]");

curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    "ACCEPT:application/json\n",
    "VERSION:2.1\n",
    "AUTHENTICATION:MY_API_KEY",
));

$result = curl_exec($curl);
curl_close($curl); 

Upvotes: 0

profitphp
profitphp

Reputation: 8354

I hate to feed the trolls, but i was bored. You really should do some legwork on these things first, and also accept (checkmark) answers when they are right, or got you really close.

<?php

$ch = curl_init();
$data = array('article' => 'This is a sample sentence, does it make you happy?', 'returnfields' => '[]');
$headers = array ('AUTHENTICATION'=>'MY_API_KEY','ACCEPT'=>'application/json','VERSION'=>'2.1');
curl_setopt($ch, CURLOPT_URL, "http://gyrus.lymbix.com/tonalize");
curl_setopt($ch, CURLOPT_HTTPHEADERS,$headers); 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
curl_close($ch);
?>

Upvotes: 1

Related Questions