Hi I am having trouble sending only byte data in a request
I only want to sent the [bytes from data stream] but am struglling in stripping this from my curl request.
Can anybody help me stripping this of via curl please.
-----------------------------7dc1f42e3005a8
Content-Disposition: form-data; name="upload"; filename="file.mp3"
Content-Type: audio/mpeg
**[bytes from data stream]**
-----------------------------7dc1f42e3005a8--
The curl that I am using below, I am wondering if there is a quick way in curl to send only a specific part of load in this case only the byte data.
$fp = fopen($localfile['tmp_name'], 'r');
$ch = curl_init();
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); // --data-binary
curl_setopt($ch, CURLOPT_URL, 'http://api.com/api/v4/track/upload?api_key=xxx&filetype=mp3');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/octet-stream'));
$response = curl_exec($ch);
you can use below solution
//your url
$url = 'http://localhost/test/test.php';
$file = realpath('php1.php');
// build multipart
//set appropriate content type in headers
$params = "--ABC1234\r\n"
. "Content-Type: application/x-www-form-urlencoded\r\n"
. "--ABC1234\r\n"
. "Content-Type: text/php\r\n"
. "Content-Disposition: attachment; filename=\"php1.php\"\r\n"
. "\r\n"
. file_get_contents($file) . "\r\n"
. "--ABC1234--";
$first_newline = strpos($params, "\r\n");
$multipart_boundary = substr($params, 2, $first_newline - 2);
$request_headers = array();
$request_headers[] = 'Content-Length: ' . strlen($params);
$request_headers[] = 'Content-Type: multipart/form-data; boundary=' . $multipart_boundary;
// send the request now
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 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);
curl_close ($ch);
on other end you will be able to get uploaded file in $_FILES variable.
Tested at local server.
Hope this will help you.
Related
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);
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 want to upload file from one server to another without using the file control and using Curl Request..
curl -X POST --header 'Content-Type: multipart/form-data' --header 'Accept: application/json' --header 'api-token: 6a53bcbe222c490196e4b9f87ba9148c' --header 'api-secret: 6a53bcbe222c490196e4b9f87ba9148c' -F "document[]=#C:/xampp/htdocs/project/image_name.ext" -F "document[]=#C:/xampp/htdocs/project/image_name.ext" -F "document[]=#C:/xampp/htdocs/project/image_name.ext" 'https://server_api_url.com
'
The above curl works fine...
But i dont know how to post the file in php ...
Below is sample code how i am sending post data using php curl
<?php
$doc_file_path_string = ' -F document[]=#image_name';
array_push($doc_data_array, $doc_file_path_string);
$url = 'https://server_url.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, implode(' ',$doc_data_array));
$headers = array();
$headers[] = "Content-Type: multipart/form-data";
$headers[] = "Accept: application/json";
$headers[] = "Api-Token: api_key";
$headers[] = "Api-Secret: api_key ";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
echo $result = curl_exec($ch);
?>
Here is how i did it
$url = <some url>;
$ch = curl_init();
$headers = ["Content-Type:multipart/form-data"];
// Define curl options
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
// Here we make base64 encode for file content and push it into curl
$base64 = base64_encode(file_get_contents(<path to file>));
curl_setopt($ch, CURLOPT_POSTFIELDS, ['file' => $base64]);
// Make request
$response = curl_exec($ch);
curl_close($ch);
I'm new to PHP/cURL and I need to add these content elements to the CURLOPT_HTTPHEADER:
Content-Type: application/octet-stream
Content-Disposition: form-data; name="media[]"; filename="http://www.example.com/pic/test.jpg"
PHP:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: ' . $this->get_DST($status)));
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'status='.rawurlencode($status);
curl_setopt($ch, CURLOPT_URL, $url);
$headers = array( 'Authorization: ' . $this->get_DST($status),
'Content-Type: application/octet-stream',
'Content-Disposition: form-data; name="media[]"; filename="http://www.example.com/pic/test.jpg'
);
curl_setopt ($ch, CURLOPT_HTTPHEADER,$headers);
$headers = array(
'Authorization: ' . $this->get_DST($status),
'Content-Type: application/octet-stream',
'Content-Disposition: form-data; name="media[]"; filename="http://www.example.com/pic/test.jpg"'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
http://www.php.net/manual/en/function.curl-setopt.php