【问题标题】:How to add product images with Prestashop API如何使用 Prestashop API 添加产品图片
【发布时间】:2020-07-27 16:19:02
【问题描述】:

我正在尝试通过 Prestashop 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/

【问题讨论】:

    标签: php api web-services curl prestashop


    【解决方案1】:

    尝试:

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

    【讨论】:

    • 我之前已经尝试过,但是我在这里根本没有得到任何 http 状态...我使用的是 PHP 版本 7.2.29。在这种情况下,CURL 的输出是:Array ( [url] => http://my-shop.com/api/images/products/24/ [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0
    • 尝试激活 Prestashop 错误,然后再试一次,因为错误 500 会突然给出更精确的错误
    • 你的意思是调试模式吗?我也试过了,但它似乎不影响 API。不知道怎么回事
    • 我试图将脚本指向其他地方,这似乎是 cURL/CurlFile 本身的问题,因为结果相同 -> 没有响应代码。我需要检查服务器配置。非常感谢您的帮助
    • 在服务器上,检查:allow_url_fopen = on
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多