PHP Curl: How to Set Body to Binary Data

PHP cURL: how to set body to binary data?

You can just set your body in CURLOPT_POSTFIELDS.

Example:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://url/url/url" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, "body goes here" );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));

$result=curl_exec ($ch);

Taken from here

Of course, set your own header type, and just do file_get_contents('/path/to/file') for body.

PHP - POST request with binary body data - Works with CURL, but not with Laravel

I would try withBody

$post = Http::withBody(file_get_contents($pathToFile), 'image/jpeg')
->withToken("<Token>")
->post($url);

How does one POST a binary file stored locally using php-curl?

you'd use

curl_setopt($curl_handle, CURLOPT_POST, TRUE);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, array('file' => '@foo.ext'));

plus whatever options you need. Relevant docs here: http://php.net/curl_setopt



Related Topics



Leave a reply



Submit