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);
Related
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 {
}
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);
I have a cURL command which works very well en CLI (via git Bash). See below:
curl -D- -k -o tvdata.xls -u adminid:adminpw -X GET -H "Content-Type: application/vnd.ms-excel" https://localhost/jira/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%3D+LPCCU+AND+type+%3D+Incident
This command export very well all issues in an excel file (xls) from my JIRA.
Now i want to transform this command to php curl. I have tried this code below:
$url = 'https://build.bnum.laposte.fr/jira/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%3D+LPCCU+AND+type+%3D+Incident';
$username ='adminid';
$password ='adminpw';
$file = fopen('tvdata.xls', 'w');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/vnd.ms-excel"));
curl_setopt($ch, CURLOPT_NOPROGRESS, FALSE);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 15000);
curl_setopt($ch, CURLOPT_FILE, $file);
curl_exec($ch);
curl_close($ch);
fclose($file);
but when i run this php code, he create an empty excel file only, but without to get an error. If somebody can figure out this problem please?
Thanks in advance
Achillix
Have you tried using on-line converters like this one?
From...
curl -D- -k -o tvdata.xls -u adminid:adminpw -X GET -H "Content-Type: application/vnd.ms-excel" https://localhost/jira/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%3D+LPCCU+AND+type+%3D+Incident
...you get:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://localhost/jira/sr/jira.issueviews:searchrequest-excel-current-fields/temp/SearchRequest.xls?jqlQuery=project+%3D+LPCCU+AND+type+%3D+Incident");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, "adminid" . ":" . "adminpw");
$headers = array();
$headers[] = "Content-Type: application/vnd.ms-excel";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
...compared with your sample code, CURLOPT_URL and CURLOPT_HTTPHEADER are different, and some other options are not set.
can any one help me on converting bash curl to php curl, its showing internal error
api_key="d6b991ecexxxxxxxxxxxxxxfedc3"
app_key="d06d8c833xxxxxxxxxxxxxf3ccf4"
curl -POST \
-d 'graph_json={"requests":[{"q":"avg:system.load.1{*}"}],"viz":"timeseries","events":[]}' \
-d "timeframe=1_hour" \
-d "size=medium" \
-d "legend=yes" \
"https://app.datadoghq.com/api/v1/graph/embed?api_key=${api_key}&application_key=${app_key}"
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $_ENV["https://app.datadoghq.com/api/v1/graph/embed?api_key={api_key}&application_key={app_key}"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "graph_json={\"requests\":[{\"q\":\"avg:system.load.1{*}\"}],\"viz\":\"timeseries\",\"events\":[]}&timeframe=1_hour&size=medium&legend=yes");
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);
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);