Convert command cURL to PHP cURL - php

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.

Related

File upload CURL command to PHP Curl

I have CURL command I need to use him in php page how I can change it. I have no idea about it.
The command :
curl --request POST \
--url 'https://api.sirv.com/v2/files/upload?filename=%2Fpath%2Fto%2Fuploaded-image.jpg' \
--header 'authorization: Bearer BEARER_TOKEN_HERE' \
--header 'content-type: image/jpeg' \
--data "#/path/to/local-file.jpg"
For your curl command, Please try this PHP :
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.sirv.com/v2/files/upload?filename=%2Fpath%2Fto%2Fuploaded-image.jpg');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array(
'file' => '#' .realpath('/path/to/local-file.jpg')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$headers = array();
$headers[] = 'Authorization: Bearer BEARER_TOKEN_HERE';
$headers[] = 'Content-Type: image/jpeg';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
?>
Please also study the reference provided by Professor Abronsius if you have time. It's worth the effort.

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.

Issue onconvreting bash curl to php curl

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

Getting cURL call to work in PHP -XPOST

I have the follwing curl call which works on linux.
curl -XPOST 'https://site.site.com/1.0/projects/1626/inbox/search.json' -H 'Content-Type: application/json' -H 'Authorization: randomauthorizationcodethatisactuallymuchlongerandmorerandom' --data '{}'
This call works when called in the terminal. I've tried to recreate it using PHP functions and I'm not having any luck. This is what I have done
$accessToken = 'randomauthorizationcodethatisactuallymuchlongerandmorerandom';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://site.site.com/1.0/projects/1626/inbox/search.json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HEADER, 'Authorization: ' . $accessToken);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
$result = curl_exec($ch);
curl_close($ch);
echo $result;
I've been messing with this one for days and I can't figure it out. Any help would be appreciated. Thank you!

Curl: Alternative Php code for Shell script

I am trying to convert shell code into php. The following code runs well when we run it through shell, but i want to write the code in php.
URL="example.com"
curl -k \ -w '\nHTTP STATUS: %{http_code}\n'\
-F "property_id[]=92cf91e5-9032-5e03-8d78-ff1e3af7d9d0" \
-F "property_id[]=6e6b2fbe-75c3-5896-aab0-6d809d6d4ac1" \
-F "property_id[]=c96d6baf-5d6c-564f-9cc4-7ed446e7785c" \
-X 'POST' "$URL"
I tried the following but I am getting "Unknown error"...
$params = "--ABC1234\r\n"
. "Content-Type: application/x-www-form-urlencoded\r\n"
. "--ABC1234\r\n"
. "Content-Type: application/x-www-form-urlencoded\r\n"
. "Content-Disposition: form-data; name=\"property_id[]\" 8219cd38-b5c9-4dc6-a119-c983cf1d844b \r\n"
. "\r\n"
. "--ABC1234--";
$request_headers[] = 'Content-Length: ' . strlen($params);
$request_headers[] = 'Content-Type: application/x-www-form-urlencoded; boundary='. $multipart_boundary;
$ch = curl_init($posturl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
$reply = curl_exec($ch);
echo "<pre>";
print_r($reply);
Thanks in advance
Finally found the solution for it. It was very simple.
$posturl="https://availability.getaroom.com/api/1.1/room_availability?transaction_id=***&check_in=08/18/2015&check_out=08/19/2015&rooms=1&adults=2&cancellation_rules=1&api_key=**************&auth_token=*********************&currency=AUD";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$posturl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"property_id=1234567890");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$getaroomsearch = new SimpleXMLElement(curl_exec($ch));
curl_close ($ch);

Categories