What's the PHP equivalent of the following CURL command?
curl -X POST -d "html=<html><body><h1 style="color: red;">Hello World!</h1>" http://example.com/post"
Try something like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com/post');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
$data = array('html' => '<html><body><h1 style="color: red;">Hello World!</h1>');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
For future reference, it'd be useful to read PHP's documentation and scout out other examples before asking this type of question.
Related
What's the PHP equivalent of the following CURL command?
curl -X POST -d "html=<html><body><h1 style="color: red;">Hello World!</h1>" http://example.com/post"
Try something like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com/post');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
$data = array('html' => '<html><body><h1 style="color: red;">Hello World!</h1>');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
For future reference, it'd be useful to read PHP's documentation and scout out other examples before asking this type of question.
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);
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
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
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.