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

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

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 {
}

How to write curl code in PHP

Curl code I am using in php but it is not working
curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/unsubscribes \
-F address='bob#example.com' \
-F tag='tag1'
try this
$ch = curl_init(); // initiate curl
$url = 'https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/unsubscribes';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "&address=" . $address . "&tag=" . $tag ); // define what you want to post
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the output in string format
$output = curl_exec($ch); // execute
curl_close($ch);
CRUL to PHP code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/unsubscribes");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, "api" . ":" . "YOUR_API_KEY");
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

Sending a put command with Curl on php

Given the following shell command:
curl -X PUT -d arg1='value1'-d arg2='value2' -d arg3='value3' \
https://api.myurl/1/subscriptions/1234;
What is the PHP Curl equivalent?
Checkout this site: https://incarnate.github.io/curl-to-php/
Created this:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "arg2='value2'");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "arg1='value1'-d&arg3='value3'");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($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

PhoneGap API Curl from Command Line to php

I want to build phonegap application using the API give on https://build.phonegap.com/docs/api
It requires the following curl command to run which will return a token.
$ curl -u andrew.lunny#nitobi.com -X POST "" https://build.phonegap.com/token
This curl statement is working for me on command line. I want to use it in php. I tried the php code given below but its not working.
$username = "USERNAME#gmail.com";
$password = "PASSWORD";
$target_url = "https://build.phonegap.com/token"
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec($ch);
curl_close ($ch);
echo $result;
You can also try the shell_exec() function:
$cmd = "curl -u andrew.lunny#nitobi.com -X POST "" https://build.phonegap.com/token";
$out = shell_exec($cmd);
$data = json_decode($out);
I have found the solution.
There is some problem on my localhost settings. When I uploaded the code on web server, it started working.

Categories