Curl: Alternative Php code for Shell script - php

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

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 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.

How to send all files from a folder by cURL in php from windows os? [duplicate]

I'm trying to upload more than one image with PHP Curl. The API I'm using gives me the following example:
curl -v -s -u username:password \
-H "Content-Type: multipart/form-data" \
-H "Accept: application/vnd.com.example.api+json" \
-F "image=#img1.jpeg;type=image/jpeg" \
-F "image=#img2.jpeg;type=image/jpeg" \
-XPUT 'https://example.com/api/sellers/12/ads/217221/images'
So in my php script I tried this:
$files = array();
foreach($photos as $photo) {
$cfile = new CURLFile('../' . $photo, 'image/jpeg', 'test_name');
$files[] = $cfile;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://services.example.com/seller-api/sellers/' . $seller . '/ads/' . $voertuig_id . '/images');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $files);
curl_setopt($ch, CURLOPT_PROXY,'api.test.sandbox.example.com:8080');
curl_setopt($ch, CURLOPT_USERPWD, 'USER:PASS');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Host: services.example.com',
'Content-type: multipart/form-data; boundary=vjrLefDejJaWiU0JzZfsadfasd1rMcE2HQ-n7XsSx',
'Accept: application/vnd.com.example.api+json'
));
$output = curl_exec($ch);
curl_close($ch);
First I got this response from the API:
{
"errors":{
"error":{
"#key":"unsupported-form-element"
}
}
}
but now there is no response at all.
How do I use curl to upload multiple files?
if $files is an empty JSON for example, it gives no error at all and returns the images (empty ofcourse).
This is the documentation of the API I'm using:
https://services.mobile.de/manual/new-seller-api.html#_upload_images
EDIT:
I tried to build the request body and send it, but it doesn't do anything:
$requestBody = '';
$requestBody .= '--vjrLeiXjJaWiU0JzZkUPO1rMcE2HQ-n7XsSx\r\n';
$requestBody .= 'Content-Disposition: form-data; name="image"; filename="ferrari.JPG"\r\n';
$requestBody .= 'Content-Type: image/jpeg\r\n';
$requestBody .= 'Content-Transfer-Encoding: binary\r\n';
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
try use assoc array, how it use into documentation CURLFile
$photos = [
'img1' => 'img1.jpg',
'img2' => 'img2.jpg'
]
$files = [];
foreach($photos as $key => $photo) {
$cfile = new CURLFile('../' . $photo, 'image/jpeg', $key);
$files[$key] = $cfile;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://services.example.com/seller-api/sellers/' . $seller . '/ads/' . $voertuig_id . '/images');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $files);
curl_setopt($ch, CURLOPT_PROXY,'api.test.sandbox.example.com:8080');
curl_setopt($ch, CURLOPT_USERPWD, 'USER:PASS');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Host: services.example.com',
'Content-type: multipart/form-data; boundary=vjrLefDejJaWiU0JzZfsadfasd1rMcE2HQ-n7XsSx',
'Accept: application/vnd.com.example.api+json'
));
$output = curl_exec($ch);
curl_close($ch);

Upload images trough API (PHP cURL) [duplicate]

I'm trying to upload more than one image with PHP Curl. The API I'm using gives me the following example:
curl -v -s -u username:password \
-H "Content-Type: multipart/form-data" \
-H "Accept: application/vnd.com.example.api+json" \
-F "image=#img1.jpeg;type=image/jpeg" \
-F "image=#img2.jpeg;type=image/jpeg" \
-XPUT 'https://example.com/api/sellers/12/ads/217221/images'
So in my php script I tried this:
$files = array();
foreach($photos as $photo) {
$cfile = new CURLFile('../' . $photo, 'image/jpeg', 'test_name');
$files[] = $cfile;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://services.example.com/seller-api/sellers/' . $seller . '/ads/' . $voertuig_id . '/images');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $files);
curl_setopt($ch, CURLOPT_PROXY,'api.test.sandbox.example.com:8080');
curl_setopt($ch, CURLOPT_USERPWD, 'USER:PASS');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Host: services.example.com',
'Content-type: multipart/form-data; boundary=vjrLefDejJaWiU0JzZfsadfasd1rMcE2HQ-n7XsSx',
'Accept: application/vnd.com.example.api+json'
));
$output = curl_exec($ch);
curl_close($ch);
First I got this response from the API:
{
"errors":{
"error":{
"#key":"unsupported-form-element"
}
}
}
but now there is no response at all.
How do I use curl to upload multiple files?
if $files is an empty JSON for example, it gives no error at all and returns the images (empty ofcourse).
This is the documentation of the API I'm using:
https://services.mobile.de/manual/new-seller-api.html#_upload_images
EDIT:
I tried to build the request body and send it, but it doesn't do anything:
$requestBody = '';
$requestBody .= '--vjrLeiXjJaWiU0JzZkUPO1rMcE2HQ-n7XsSx\r\n';
$requestBody .= 'Content-Disposition: form-data; name="image"; filename="ferrari.JPG"\r\n';
$requestBody .= 'Content-Type: image/jpeg\r\n';
$requestBody .= 'Content-Transfer-Encoding: binary\r\n';
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
try use assoc array, how it use into documentation CURLFile
$photos = [
'img1' => 'img1.jpg',
'img2' => 'img2.jpg'
]
$files = [];
foreach($photos as $key => $photo) {
$cfile = new CURLFile('../' . $photo, 'image/jpeg', $key);
$files[$key] = $cfile;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://services.example.com/seller-api/sellers/' . $seller . '/ads/' . $voertuig_id . '/images');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $files);
curl_setopt($ch, CURLOPT_PROXY,'api.test.sandbox.example.com:8080');
curl_setopt($ch, CURLOPT_USERPWD, 'USER:PASS');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Host: services.example.com',
'Content-type: multipart/form-data; boundary=vjrLefDejJaWiU0JzZfsadfasd1rMcE2HQ-n7XsSx',
'Accept: application/vnd.com.example.api+json'
));
$output = curl_exec($ch);
curl_close($ch);

Upload multiple images with PHP Curl

I'm trying to upload more than one image with PHP Curl. The API I'm using gives me the following example:
curl -v -s -u username:password \
-H "Content-Type: multipart/form-data" \
-H "Accept: application/vnd.com.example.api+json" \
-F "image=#img1.jpeg;type=image/jpeg" \
-F "image=#img2.jpeg;type=image/jpeg" \
-XPUT 'https://example.com/api/sellers/12/ads/217221/images'
So in my php script I tried this:
$files = array();
foreach($photos as $photo) {
$cfile = new CURLFile('../' . $photo, 'image/jpeg', 'test_name');
$files[] = $cfile;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://services.example.com/seller-api/sellers/' . $seller . '/ads/' . $voertuig_id . '/images');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $files);
curl_setopt($ch, CURLOPT_PROXY,'api.test.sandbox.example.com:8080');
curl_setopt($ch, CURLOPT_USERPWD, 'USER:PASS');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Host: services.example.com',
'Content-type: multipart/form-data; boundary=vjrLefDejJaWiU0JzZfsadfasd1rMcE2HQ-n7XsSx',
'Accept: application/vnd.com.example.api+json'
));
$output = curl_exec($ch);
curl_close($ch);
First I got this response from the API:
{
"errors":{
"error":{
"#key":"unsupported-form-element"
}
}
}
but now there is no response at all.
How do I use curl to upload multiple files?
if $files is an empty JSON for example, it gives no error at all and returns the images (empty ofcourse).
This is the documentation of the API I'm using:
https://services.mobile.de/manual/new-seller-api.html#_upload_images
EDIT:
I tried to build the request body and send it, but it doesn't do anything:
$requestBody = '';
$requestBody .= '--vjrLeiXjJaWiU0JzZkUPO1rMcE2HQ-n7XsSx\r\n';
$requestBody .= 'Content-Disposition: form-data; name="image"; filename="ferrari.JPG"\r\n';
$requestBody .= 'Content-Type: image/jpeg\r\n';
$requestBody .= 'Content-Transfer-Encoding: binary\r\n';
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
try use assoc array, how it use into documentation CURLFile
$photos = [
'img1' => 'img1.jpg',
'img2' => 'img2.jpg'
]
$files = [];
foreach($photos as $key => $photo) {
$cfile = new CURLFile('../' . $photo, 'image/jpeg', $key);
$files[$key] = $cfile;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://services.example.com/seller-api/sellers/' . $seller . '/ads/' . $voertuig_id . '/images');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $files);
curl_setopt($ch, CURLOPT_PROXY,'api.test.sandbox.example.com:8080');
curl_setopt($ch, CURLOPT_USERPWD, 'USER:PASS');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Host: services.example.com',
'Content-type: multipart/form-data; boundary=vjrLefDejJaWiU0JzZfsadfasd1rMcE2HQ-n7XsSx',
'Accept: application/vnd.com.example.api+json'
));
$output = curl_exec($ch);
curl_close($ch);

Categories