简体   繁体   中英

PHP curl post image from URL

Currently, to POST image from local folder I use curl and PHP, here is my code:

$url = 'http://myprestashop.com/api/images/products/1';

$image_path = 'C:\\my_image.png';
$key = 'My web service key';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, $key.':');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '@'.$image_path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

How can I POST image from URL ( http://imageserver.com/image.jpeg ) instead of local folder (C:\\my_image.png)? Is it possible?

Just change your image path and try this code.

$url = 'http://myprestashop.com/api/images/products/1';

$data = array('key' => 'My web service key', 'image_path' => '@/home/user/test.png');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

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