【问题标题】:Add product images automatically with CURL php使用 CURL php 自动添加产品图片
【发布时间】:2017-02-11 02:31:27
【问题描述】:

我一直在尝试将带有 curl 的图像添加到我的所有产品中,但没有成功。 我的图片位于 www.myshop.com/prestashop/img/ProductImages (我已在此处上传)。

对于产品每个产品 i (id = ID_i, ref = REF_i),图像称为 REF_i.jpg

这是我一直在尝试做的:

 <?php

function image($id, $ref){
   // change the local path where image has been downloaded (I run script at the folder before prestashop)
    $img_path = '/prestashop/img/ProductImages/{$ref}.jpg';


define('PS_SHOP_PATH', 'http://craftypapersco.wt.pt/prestashop/');
//image will be associated with product id $id
$url = PS_SHOP_PATH. '/api/images/products/'.$id;


$ch = curl_init();


curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, PS_WS_AUTH_KEY.'SOMEKEY');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image'=> new CurlFile($img_path)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


if(curl_exec($ch) === false)
{
  echo "<br><br>Error : ".curl_error($ch)."<br>";
}
else { echo '<br><br> Image added'; }
curl_close($ch);


}


ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
define('PS_SHOP_PATH', 'http://craftypapersco.wt.pt/prestashop/');
define('PS_WS_AUTH_KEY', 'SOMEKEY');
define('DEBUG', 'false');


require_once('PSWebServiceLibrary.php');
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);


$opt['resource'] = 'products';
$opt['display'] = 'full';
try{
    $xml = $webService->get($opt);
}catch( PrestaShopWebserviceException $ex ){
    $trace = $ex->getTrace(); // Retrieve all information on the error
     $errorCode = $trace[ 0 ][ 'args' ][ 0 ]; // Retrieve the error code
     if ( $errorCode == 401 )
           echo 'Bad auth key';
     else
           echo 'Other error';
    }
    foreach($xml->product as $product){
        image($product->id, $product->reference)
    }

?>

你能帮我弄清楚为什么它不起作用吗?

谢谢, 卡罗莱纳州

【问题讨论】:

  • 您收到的错误信息是什么?
  • 它什么也没做。 BVengerov 指出了一个错字,我会更正它并在此处发布结果(我之前没有这个问题,它也失败了)。
  • 好的,我已经纠正了这个错误,但仍然没有任何反应......我得到一个空白页

标签: php curl web service prestashop


【解决方案1】:

这里只是一个猜测答案(准备在更多信息到达后进行更多编辑)。

如果您提供的代码 100% 相同,那么您有一个 未封闭的引号,它会弄乱文件的结尾。这里:

echo 'Other error :

【讨论】:

  • 谢谢,我已经更正了错误,但仍然没有任何反应...我得到一个空白页
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-23
  • 1970-01-01
  • 2020-07-27
  • 2018-07-04
  • 2023-01-01
  • 1970-01-01
  • 2020-08-09
相关资源
最近更新 更多