简体   繁体   中英

Send an image with CURL

Is it possible to send an image with CURL that outputs with ImagePNG?

ob_start();
imagepng(imagecreatetruecolor(800, 600));
$file = ob_get_clean();

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $remoteHost);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, [
    'file1' => ?
]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($curl);
curl_close($curl);

Thank you!

You just need to put @ character before the file path, and the path must be the full path.

curl_setopt($curl, CURLOPT_POSTFIELDS, array(
    'file1' => '@' . '/var/tmp/img.png'
));

If the request accepts the image inside the POST body as raw data, that case you can just place it inside the POST with mentioning whatever content-type it requires.

$image_data = file_get_contents('/var/tmp/img.png');
curl_setopt($curl, CURLOPT_POSTFIELDS, $image_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));  // just an example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM