Help with CURL in PHP - php

I'm trying to code this in PHP:
curl -F 'access_token=123' \
-F 'message=Hello, Arjun. I like this new API.' \
https://graph.facebook.com/arjun/feed
But I have no idea how to do it...
Any ideas?
Thanks
TheBounder.

You can't execute curl like this, you must first install a PHP extension (cUrl) (look here for documentation).
Then you can do things like this:
$url="https://graph.facebook.com/arjun/feed";
$ch = curl_init();
$vars = "message=YourMessage";
if($ch === FALSE){
echo "Errore init curl";
}else{
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
$returned = curl_exec($ch);
$returned = html_entity_decode($returned);
curl_close ($ch);
I just copied some code that i used some time ago, take it as a starting point.

Related

How to convert curl "-b" tag into php curl?

I try to convert this curl command (proxmox api) into php curl : curl -k -b "PVEAuthCookie=PVE:root#pam:4EEC61E2::rsKoApxDTLYPn6H3NNT6iP2mv..." https://10.0.0.1:8006/api2/json/
I used the site https://incarnate.github.io/curl-to-php/ to convert it but it doesn't support the "-b" tag.
After some search, I tried this code :
<?php
$apiurlvmName = "https://10.0.0.1:8006/api2/json/";
$proxmoxid = "username=root#pam&password=mypass";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiurlvmName);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $proxmoxid);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_COOKIE, "PVEAuthCookie=PVE:root#pam:4EEC61E2::rsKoApxDTLYPn6H3NNT6iP2mv...");
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
else {
echo $result;
}
curl_close($ch);
?>
But it doesn't works. The page takes few seconds to load then I get a blank page without error displayed. As if "curl_setopt($ch, CURLOPT_COOKIE, "PVEAuthCookie=PVE:root#pam:4EEC61E2::rsKoApxDTLYPn6H3NNT6iP2mv...");" were not enough.
Did I missed something ? How can I convert the curl "-b" tag correctly into php ? Thank you.
It has already been answered hundreds of times. But just in case, let's copy it here again for those who can't find it.
-b means "COOKIE".
# sending manually set cookie
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: PVEAuthCookie=PVE:root#pam:4EEC61E2::rsKoApxDTLYPn6H3NNT6iP2mv..."));
# sending cookies from file
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);

How to implement cURL on these parameters

please I need your help.
I am working on a plagiarism api from Prepostseo, and I have been given this parameters to invoke using cURL. Now, I know little of cURL because I have been using file_get_contents. But now I am required to only use cURL. I have searched through their documentation, no reference material or source code available, not even on Github.
Here are the parameters, I need help please, on how to implement this:
curl -X POST https://www.prepostseo.com/apis/checkSentence \
-d "key=YOUR_KEY"
-d "query=Inside that cage there was a green teddy bear"
Thanks in advance!
For future reference, you can use the https://incarnate.github.io/curl-to-php
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.prepostseo.com/apis/checkSentence");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "key=YOUR_KEY&query=Inside that cage there was a green teddy bear");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
echo $result;
?>
This link explains everything you need to know about how to use cURL in PHP.
The code snippet below will POST through a URL-encoded query string to the specified URL.
When the cURL call is performed, the response is assigned to the $respsonse variable, and the cURL call is closed there after.
$payload = [
'key' => 'YOUR_KEY',
'query' = 'Inside that cage there was a green teddy bear'
];
$url = "https://www.prepostseo.com/apis/checkSentence";
//set up cURL - below is a general basic set up
$ch = curl_init( $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
//specify your method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//for the body values you wish to POST through
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
//specifiy any specific headers you need here in your array
curl_setopt($ch, CURLOPT_HTTPHEADER, []);
//execute and close cURL
$response = curl_exec($ch);
curl_close($ch);

cURL to PHP equivalent

I am trying to correctly convert a cURL command to PHP, but I can't seem to find the correct commands. I can run this command with cURL but I need to run this through TROPO, and they only have a few script options.
So I am trying in PHP.
curl https://api.particle.io/v1/devices/310029000547353138383138/setNum \ -d access_token=e650d0dec0476de7d64b23110fed31dcb3cbxxxx \ -d args=2085555555
My try, what am I missing?
<?php
function submitValue() {
$ch = curl_init("https://api.particle.io/v1/devices/310029000547353138383138/setNum");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, 'access_token=e650d0dec0476de7d64b23110fed31dcb3cbxxxx&"args=2085555555"');
);
$output = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != '200') {
return null;
}
return $output;
}
?>
Try to use cURL command below:
curl --data "access_token=e650d0dec0476de7d64b23110fed31dcb3cbxxxx&args=2085555555" https://api.particle.io/v1/devices/310029000547353138383138/setNum
Got this answer from https://superuser.com/a/149335
UPDATED:
Here I have provided PHP code:
$data = json_decode(array(
"access_token" => "e650d0dec0476de7d64b23110fed31dcb3cbxxxx",
"args" => "2085555555"
));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.particle.io/v1/devices/310029000547353138383138/setNum');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
Hope this will help

Converting a Curl command into PHP Curl code (file transfer & custom command)

I am trying to convert the following Curl command:
curl --digest --user "username:password" --verbose --url "http://127.0.0.1/ws?graph-uri=http://localhost/dataset/import/" -X POST -T /data/datasets/foo.n3
Here is the code that appears to be ok, but that doesn't work:
$args = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1/ws?graph-uri=http://localhost/dataset/import/');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
$args['graph-uri'] = curl_file_create('/data/datasets/foo.n3');
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$result = curl_exec($ch);
It seems that the file is actually not uploaded with the PHP code, but everywhere I look the usage of curl_file_create() seems good.
Try this previous answer, substituting graph-uri for file_contents. Also, you are setting "graph-uri" as both GET and POST params, which could collide, and the graph-uri GET param is not URL encoded, which could behave differently in the shell vs. PHP. So you might try the following:
http://127.0.0.1/ws?graph-uri=http%3A%2F%2Flocalhost%2Fdataset%2Fimport%2F

php: curl script working but not in php file

I have a curl script that is working fine in terminal.
curl -XPOST \
-F "file=#var/www/myfile.wav;type=audio/wav" \
-H 'Authorization: Bearer $MY_TOKEN' \
'https://myurl'
I have tried this way in my php file,
<?php
$url = "https://myurl";
$key = "myKey";
$path = "/var/www/";
$fileName = "myfile.wav";
$data['file']= "#".$path.$fileName;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Authorization: Bearer $key"));
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
I get an error from the server. Seems I am not making request as the server expects. Am I missing anything in the php script?
Try this:
$data = array('file' => file_get_contents($path.$fileName));
There may be a problem with your CURL library that PHP is using.
Or PHP may be blocked from making HTTPS connections.
Try a series of simple tests using CURL in PHP like this first one with POST false:
<?php
$url = "https://myurl/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
header("Content-Type: text/plain");
echo $response;
?>
and then with POST true...
<?php
$url = "https://myurl/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
header("Content-Type: text/plain");
echo $response;
?>
Since it's an HTTPs URL , You need to make use of this parameter
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Categories