如何使用 Prestashop 添加产品图片 API

How to add product images with Prestashop API

我正在尝试通过 Prestashop add/upload 产品图片 API,但我收到服务器错误 500。 代码有什么问题?或者服务器配置有问题?

PHP 脚本:

error_reporting(-1);
ini_set('display_errors', 'On');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://MY_AUTH_KEY@my-shop.com//api/images/products/24/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, 'MY_AUTH_KEY:');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' =>'@http://my-shop.com/img/my-shop-logo-1584646645.jpg;type=image/jpg'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
curl_close($ch);

print_r($curlinfo);

这导致 [http_code] => 500。没有错误或任何错误。我可以访问托管服务提供商的服务器错误日志,但其中没有任何内容...

该脚本基于 Prestashop 文档:https://devdocs.prestashop.com/1.7/development/webservice/tutorials/change_product_image/

试试:

error_reporting(-1);
ini_set('display_errors', 'On');

$image_path = 'http://my-shop.com/img/my-shop-logo-1584646645.jpg';
$image_mime = 'image/jpg';

$args['image'] = new CurlFile($image_path, $image_mime);

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_URL, 'http://my-shop.com/api/images/products/24/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, 'MY_AUTH_KEY:');
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
curl_close($ch);

print_r($curlinfo);

我找到了答案 - 您无法使用 cURL 上传远程文件。它只能发送本地文件。所以你需要下载文件,将其保存在本地,然后使用带有本地路径的 cURL。

来源: How can I use cURL's '@' syntax with a remote URL?