cURL to PHP equivalent - php

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

Related

PHP Implementation for ACINQ/Strike API

I need to implement the following curl using PHP.
$ curl https://api.dev.strike.acinq.co/api/v1/charges \
-u sk_pJDwxFxCVw5fQJhRRMpf29jReUjjN: \
-X POST \
-d amount=42000 \
-d currency="btc" \
-d description="1%20Blockaccino"
This is what I have so far.
$post=array();
$post["amount"]=4200;
$post["currency"]="btc";
$post["description"]="1%20Blockaccino";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERNAME, "sk_pJDwxFxCVw5fQJhRRMpf29jReUjjN:");
curl_setopt($ch, CURLOPT_URL, "https://api.dev.strike.acinq.co/api/v1/charges");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$output = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http==200) {
} else {
}
It returns
{ "code" : 401, "message" : "authentication failed" }
Even though the CURLOPT_USERNAME is accurate. Are there other issues I am not seeing?
Thanks in advance.
CURLOPT_USERNAME was added in PHP 5.5.0.
If you have an older version then you may want to try:
curl_setopt($ch, CURLOPT_USERPWD, "sk_pJDwxFxCVw5fQJhRRMpf29jReUjjN:");
Here is the final WORKING solution...
$post=array();
$post["amount"]=100000;
$post["currency"]="btc";
$post["description"]="test";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERNAME, "sk_XXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
curl_setopt($ch, CURLOPT_URL, "https://api.strike.acinq.co/api/v1/charges");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-Type: application/json"));
$output = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http==200) {
} else {
}

Need help to convert cURL Command Line to run from PHP file

The cURL command that I run from the command line is:
curl --digest -u "username:password" "http://[ip-address]:8086/streammanager/streamAction?action=resetStream" --data "vhostName=_defaultVHost_&appName=rtr_planeta/_definst_&streamName=rtr_planeta.stream"
The problem I am facing is with the following part of the command:
--data "vhostName=_defaultVHost_&appName=rtr_planeta/_definst_&streamName=rtr_planeta.stream"
Where in PHP code do I specify --data?
I got this far:
<?php
$url="http://[ip-address]:8086/streammanager/streamAction?action=resetStream";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
$result = curl_exec($ch);
if(curl_errno($ch))
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo $result;
}
curl_close($ch);
?>
$param = 'vhostName=_defaultVHost_&appName=rtr_planeta/_definst_&streamName=rtr_planeta.stream';
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);

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);

Curl request from shell

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

Help with CURL in 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.

Categories