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

问题描述 投票:0回答:2

我正在尝试通过Prestashop API添加/上传产品图片,但出现服务器错误500。代码有什么问题?也许服务器配置有问题?

PHP脚本:

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

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://[email protected]//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/

php api web-services curl prestashop
2个回答
0
投票

尝试使用:

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

0
投票

我找到了答案-您无法使用cURL上传远程文件。它只能发送本地文件。因此,您需要下载文件,将其保存在本地,然后将cURL与本地路径一起使用。

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

© www.soinside.com 2019 - 2024. All rights reserved.