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>";
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>";
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.
Open your php.ini, situated in apache\bin.
Uncomment ;extension=php_curl.dll.
Restart Apache.
Hope this will help
Related
I'm trying to convert command-line cURL to PHP cURL, but it doesn't seem to work when I do it through PHP. I access the same URL and everything, but the API returns a general error with no data.
This works properly, it is accessed via POST with no post body, just parameters in the URL.
$curlRequest = 'curl -X POST -d "" --globoff \''.$baseURL.'?access_key='.$access_key.'&handles='.$handles.'&contextToken='.$contextToken.'&access_sig='.$access_sig.'\'';
exec($curlRequest, $result);
However, when I try to perform the same request with the cURL library in PHP, it doesn't work.
$url = $baseURL.'?access_key='.$access_key.'&handles='.$handles.'&contextToken='.$contextToken.'&access_sig='.$access_sig.'\'';
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$server_output = curl_exec($ch);
curl_close ($ch);
$server_output just ends up being an empty error from the API instead of the same data that the first code block returns. Am I doing something incorrectly? Are the two requests not equivalent?
I am trying to connect to an echosign api which used to work and now has suddenly stop working, I have tried debugging but can't seem to resolve it, this is my code
public function get_access_token_get()
{
$echoSign = new EchoSign();
$ch = curl_init('https://secure.echosign.com:443/api/rest/v2/auth/tokens');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($echoSign->echosign_creds));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = json_decode(curl_exec($ch));
curl_close($ch);
echo "<pre>"; print_r($result); echo "</pre>"; exit();
return $result->accessToken;
}
I have tested your curl code with other urls, and its working fine for them, then i tried to call your url with other curl methods and it gives empty response, so I believe you should check with the echosign support to see whats wrong.
I am using the following code to get information about a repo using the Github API.
I am using cURL, but I'm not sure how to just get the name of the repository. So, how can I get just one string from the response and echo that without echoing the complete response? I tried doing $data['name'] but that didn't work.
code:
$ch = curl_init();
curl_setopt($ch,CURLOPT_USERAGENT,'Content-type: application/json');
curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/repos/ruby/ruby');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
This request returns JSON, so just use json_decode.
$jsonStr = curl_exec($ch);
curl_close($ch);
$json = json_decode($jsonStr, true);
var_dump($json['name']);
var_dump($json['full_name']);
It should be trivial from here to get the elements you're interested in.
I have a simple PHP Script, that uses CURL to send a HTTP Post Request to a remote server.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://91.250.77.10/test.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('a' => 'b'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error)
echo $error;
else
echo $contents;
This results in an error: "no response from server". The request can't be found in the access log of the remote server, too!
What's more, if I send the postfields as a querystring, i.e.:
curl_setopt($ch, CURLOPT_POSTFIELDS, '&a=b');
then everything is just fine.
It seems like something is wrong with the Apache or PHP configuration on the remote server. Any hints?
Edit:
As for now, it looks like the Server doesn't accept (or correctly handle) requests with Content-Type: multipart/form-data
(CURL uses that type when setting an array as the postfields, but not when setting a string.)
Since I need to send a file with the request, i have to use the multipart/form-data. So how do I get the server to correctly handle this?
If you pass an array to CURLOPT_POSTFIELDS then the form is submitted as multipart/form-data There could be a possibility your remote end is not accepting this encoding type.
Best way to do POST request is to format postFields like this.
VARIABLE = VALUE separated by &.
TIP:
$postFields = 'var1=cons1&var2=cons2&var3=cons3&var4=cons4&var5=cons5';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://91.250.77.10/test.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error)
echo $error;
else
echo $contents;
Would be Pleased to do any more help.
Check if the HTTP header contains "Expect:100-continue" field. Some Servers do not deal with this field and just wait.
You can try:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));
For more information, pls see:
curl http 100 continue and multipartform-data post
I'm trying to use Neo4js Traverser via the HTTP API.
If I use it via curl on the command line it works fine, but when I try to use it via curl through PHP I get an error all the time.
This is curl command:
curl -H Accept:application/json -H Content-Type:application/json -X POST -d '{"order":"depth first"}' http://localhost:7474/db/data/node/5/traverse/node
And this is my PHP Code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:7474/db/data/node/5/traverse/node");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept:application/json',
'Content-Type:application/json'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, '"{"order": "depth first"}"');
$output = curl_exec($ch);
echo '<pre>';
var_dump(curl_getinfo($ch));
var_dump($output);
curl_close($ch);
This is the error I get:
HTTP ERROR 500
Problem accessing
/db/data/node/5/traverse/node. Reason:
java.lang.String cannot be cast to java.util.Map
Any Ideas?
Looks like you have quotes before the JSON string:
curl_setopt($ch, CURLOPT_POSTFIELDS, '"{"order": "depth first"}"');
Might want to try this:
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"order": "depth first"}');
EDIT: Although better yet, I'd use json_encode with an associative array to ensure proper escaping if necessary:
$json_data = array("order" => "depth first");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($json_data));