How to add content to HTTP header? - php

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

Related

how to implement soap request in php?

I searched this website for a helpful response for integrating a SOAP Request in PHP, but I didn't find anything that solved my problem.
I'm new to SOAP and can't figure out why I am not getting any response:
<?php
$xml = '<?xml version="1.0" encoding="utf-8"?>'.
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'.
' xmlns:xsd="http://www.w3.org/2001/XMLSchema"'.
' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'.
'<soap:Body>'.
'<food xmlns="http://www.localhost:81/dbWIP/xml1.xml">'.
'<price>$5.95</price>'.
'</food>'.
'</soap:Body>'.
'</soap:Envelope>';
$url = "http://www.localhost:81/dbWIP/xml1.xml";
$ch = curl_init();
echo $ch;
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, 0);
$headers = array();
array_push($headers, "Content-Type: text/xml; charset=utf-8");
array_push($headers, "Accept: text/xml");
array_push($headers, "Cache-Control: no-cache");
array_push($headers, "Pragma: no-cache");
array_push($headers, "SOAPAction:http://www.localhost:81/dbWIP/xml1.xml");
if($xml != null) {
echo $xml;
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml");
array_push($headers, "Content-Length: " . strlen($xml));
}
curl_setopt($ch, CURLOPT_USERPWD, "user_name:password"); /* If required */
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
echo $response;
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo"<br/> CODE:"$code;
curl_close($ch);
?>
I think you are missing URL from curl_init which will initialize a cURL session, but yours has no URL.
$curl = curl_init("http://www.localhost:81/dbWIP/xml1.xml");
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
//curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
And try setting your headers to
$headers = array(
'Content-type: text/xml;charset="utf-8"',
'Accept: text/xml',
'Cache-Control: no-cache',
'Pragma: no-cache',
'Content-length: ' . strlen($xml),
);
Try to remove the quotation marks:
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);

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

Send byte data in the request

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.

Execute SOAP using cURL

I am trying to execute an SOAP-function using cURL (because I get an error using the SoapClient().
This is my code (that is halfway working)
$credentials = "username:pass";
$url = "https://url/folder/sample.wsdl";
$page = "/folder";
$headers = array(
"POST ".$page." HTTP/1.0",
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"customerSearch\"",
"Authorization: Basic " . base64_encode($credentials)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
$data = curl_exec($ch);
The problem is that the SOAP-action is not being performed. And I also need to pass arguments to the action. Is this even possible?
You need to specify the cURL options to POST, and set the body of the request - if your not sending a body, there's no point in a POST request (and more importantly, it isn't SOAP). Building a complete HTTP request header just won't cut it.
$credentials = "username:pass";
$url = "https://url/folder/sample.wsdl";
$body = ''; /// Your SOAP XML needs to be in this variable
$headers = array(
'Content-Type: text/xml; charset="utf-8"',
'Content-Length: '.strlen($body),
'Accept: text/xml',
'Cache-Control: no-cache',
'Pragma: no-cache',
'SOAPAction: "customerSearch"'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
// Stuff I have added
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $credentials);
$data = curl_exec($ch);

Categories