I have simple php script.
$url = 'http://test.com/api/images/products/33';
$image_path = '/srv/images/some.jpg';
$key = 'qwerty';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_PUT, true); // Un-commet to edit an image
curl_setopt($ch, CURLOPT_USERPWD, $key.':');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '#'.$image_path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
The script works well. I want to convert this code to simple curl shell command.
I tried
curl -v -X POST -d image=#/srv/images/some.jpg \
http://qwerty#test.com/api/images/products/33
but error is occured. What is wrong?
you should use -F (for multipart/form-data) instead of -d (which uses application/x-www-form-urlencoded):
so this should work:
curl -v -X POST -F image=#/srv/images/some.jpg http://qwerty#test.com/api/images/products/33
Related
I'm trying to integrate a product and the example curl request is as follows.
curl https://a.klaviyo.com/api/v1/lists -G \
-d api_key=pk_e29b4ec921f6aed9a70eb1e6993bb5caed
What I don't understand is what does -G and -d signify and how do I translate this request into PHP code?
-G stand for a get request and -d is the data passed to the get
in php you do
file_get_contents('https://a.klaviyo.com/api/v1/lists?api_key=pk_e29b4ec921f6aed9a70eb1e6993bb5caed');
-G, --get
When used, this option will make
all data specified with -d, --data, --data-binary or --data-urlencode
to be used in an HTTP GET request instead of the POST
request that otherwise would be used. The data will be appended to the URL with a '?' separator.
If used in combination with -I, --head, the POST data will instead be appended to the URL with a HEAD request.
If this option is used several times, only the first one is used. This is because undoing a GET doesn't make sense, but you should then instead enforce the alternative method you prefer.
$ch = curl_init();
$data = array('api_key'=>"pk_e29b4ec921f6aed9a70eb1e6993bb5caed");
curl_setopt($ch, CURLOPT_URL, "https://a.klaviyo.com/api/v1/lists");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
Just try this code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://a.klaviyo.com/api/v1/lists?api_key=pk_e29b4ec921f6aed9a70eb1e6993bb5caed');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
// Decode the response into a PHP associative array
$response = json_decode($response, true);
echo "<pre>";print_r($response);
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 want to know, how to convert linux curl command to PHP. This is my linux curl command. Thanks in advance.
curl -i -F api_password=<YOUR_API_PASSWORD> -F file=#<LOCAL_FILE_PATH> https://upload.wistia.com/
Here is the starting point that you can use...
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $THE_REMOTE_URL_YOU_ARE_POSTING_TO);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"file" => "#c:\\file_location.txt", // note the double \\ when used within double quotes
'api_password' => 12345
));
$response = curl_exec($ch);
?>
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 have a curl command that I can successfully run from a Linux (ubuntu) command line.
I would need to incorporate this curl command in a PHP script.
How can I translate the below curl command so that it works in a PHP script?
curl -X POST -H "Accept: application/rdf+xml" -F recipe=http://demo/recipe_alta -F input=#prova_rdf.rdf http://site.cs.unibo.it/stanbol/refactor
I've been trying a lot of calls in PHP I've found on the internet but I always get http 409 and 415 messages.
I tried also exec and shell_exec but I get the same errors.
this is another call, just for posting the rdf file on the url, and i get the same errors.
$url = "http://bernina.cs.unibo.it/stanbol/triplestore/grafotest_cavo";
$post_data['file'] = "#prova_rdf.rdf";
$ch = curl_init();
$headers = array('Accept: application/rdf+xml');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$response = curl_exec($ch);
echo $response;