Issue onconvreting bash curl to php curl - php

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

Related

Convert command cURL to PHP cURL

I've never done any curl before so am in need of some help. I've tried to work this out from examples but cannot get my head around it!
How can I translate this curl command so that it works in a PHP script?
curl -v -H "Content-Type: text/xml" --cert WST279925._.1.pem --key WST279925._.1.key --pass fryeBu23^< -u WST279925._.1:KaWwNI31=$ --url https://test.ipg-online.com/ipgapi/services -d #request.xml --trace-ascii "trace.log" -o "response.xml" --insecure
My code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://test.ipg-online.com/ipgapi/services');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array(
'file' => '#' .realpath('request.xml')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_USERPWD, 'WST279925._.1' . ':' . $_ENV["KaWwNI31="]);
$headers = array();
$headers[] = 'Content-Type: text/xml';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
how do i add digital certificate and key?
Thanks for any help.

Bash CURL to php

I have some trouble with translate bash curl to PHP. Look's it is not working for me. I don't know how to check it is correct code.
curl -X "POST" "https://someurl.com/oauth/token" \
-H "Authorization: Basic eyJ...V6w" \
--data-urlencode "grant_type=password" \
--data-urlencode "scope=api" \
--data-urlencode "username=user#example.uri" \
--data-urlencode "password=p...d"
I have tryied with that:
$query1 = urlencode("grant_type=password");
$query2 = urlencode("scope=api");
$query3 = urlencode("username=login#mail.com");
$query4 = urlencode("password=loginpass");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://someurl.com/oauth/token&".$query1."&".$query2."&".$query3."&".$query4."");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Authorization: Basic ZnVydGFzdGljLTZhMTAzNTE4Y2YyOGNhNmI3OTNhYzljNmJjM";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
echo "</pre>";
print_r($result);
echo "</pre>";
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
You can use this way. Keep in mind that you are posting the parameters. So the query string should not be attached to the URL but sent as post parameters
$data = array(
'grant_type' => 'password',
'scope' => 'api',
'username' => 'user#example.uri',
'password' => 'p...d'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://someurl.com/oauth/token");
$headers = array();
$headers[] = "Authorization: Basic ZnVydGFzdGljLTZhMTAzNTE4Y2YyOGNhNmI3OTNhYzljNmJjM";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
http_build_query($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
echo $response = curl_exec($ch);

Convert Syntax from cURL to php cURL

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.

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

Get Access Token via Curl(Paypal)

I can't figure out what is missing. I want to output a sample response from the paypal REST API.
Curl example :
curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp" \
-d "grant_type=client_credentials"
My Code:
<?php
define("API_USER","EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp");
define("API_PASS","EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp");
function getAccessToken() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/oauth2/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, ("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
$output = curl_exec($ch);
echo $output;
}
?>

Categories