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);
Related
The normal curl way, works well.
curl \
-F "smfile=#test.png" \
-H "Content-Type: multipart/form-data" \
https://sm.ms/api/v2/upload
But in my PHP version curl, it returns bool(false) and string(0) "":
<?php
$url = "https://sm.ms/api/v2/upload";
$headers = array();
array_push($headers, "Content-Type: multipart/form-data");
array_push($headers, "User-Agent: ".$_SERVER['HTTP_USER_AGENT']);
// $fields = array('smfile' => curl_file_create('test.png', 'image/png', 'test.png'));
$fields = array('smfile' => new CURLFile('test.png', 'image/png', 'tset.png'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
var_dump(curl_exec($ch));
var_dump(curl_error($ch));
What's wrong with my code? ć½(*ć>Š<)oć
Try this one:
<?php
$url = "https://sm.ms/api/v2/upload";
// I guess the file is in the same directory as this script
$file = __DIR__.'/test.png';
$headers = [
'Content-Type: multipart/form-data',
'User-Agent: '.$_SERVER['HTTP_USER_AGENT'],
];
$fields = [
'smfile' => new CURLFile($file, 'image/png')
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
var_dump(curl_exec($ch));
var_dump(curl_error($ch));
?>
you can use this simple code to upload files
$explode = explode('.', $_FILES['file']['name']);
$ext = $explode[count($explode) - 1];
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
$result = move_uploaded_file($_FILES['file']['tmp_name'],
'uploads/'.basename($_FILES['file']['name']));
echo $result === true ? 'File uploaded successfuly' : 'There are some errors';
}
else
{
echo 'No File uploaded';
}
I'm trying to get in PHP this curl:
curl -X POST -u "apikey:MY_API_KEY" \
--header "Content-Type: text/plain;charset=utf-8" \
--header "Accept: application/json" \
--data-binary "MY_TEXT" \
"https://MY_DIRECTION"
so far I came up with this:
$curl = curl_init();
$post_args = array('data-binary' => $MY_TEXT );
$header_args = array(
'Content-Type: text/plain;charset=utf-8',
'Accept: application/json'
);
$apiKey = '$MY_API_KEY';
$api_args = array('apikey: ' . $apiKey);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $api_args);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_args);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header_args);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_URL, $MY_DIRECTION);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);
json_decode($result, true);
I'm trying to use Personality Insights service of IBM.
Let's try with this-
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://MY_DIRECTION');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "MY_TEXT");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'apikey' . ':' . 'MY_API_KEY');
$headers = array();
$headers[] = 'Content-Type: text/plain;charset=utf-8';
$headers[] = 'Accept: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
What would be the php curl version of the below command
curl -X PUT -H 'Content-Type: text/csv' -H 'Accept: application/json' -d #file.csv [URL]
I have the below php curl version which doesn't work
$headers = [
'Content-Type: text/csv',
'Accept: application/json'
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($filename));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
Try below code
$headers = [
'Content-Type: text/csv',
'Accept: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ''.$url'');
curl_setopt($ch, CURLOPT_PUT, 1);
$fp = fopen($file_path, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response= curl_exec ($ch);
fclose($fp);
There is a tool to convert cURL command to PHP version of cURL. You can try this website: curl-toPHP.
This is the example output using your cURL command.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'your-post-remote');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$post = array(
'file' => '#' .realpath('file.csv')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
$headers = array();
$headers[] = 'Content-Type: text/csv';
$headers[] = 'Accept: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
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);
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);