使用CURL php自动添加产品图像

时间:2016-10-03 07:42:03

标签: php curl web service prestashop

我一直试图将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)
    }

?>

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

谢谢, 卡罗来纳州

1 个答案:

答案 0 :(得分:0)

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

如果你提供的代码是100%相同,那么你有一个未封闭的引用,这会搞乱文件的结尾。这里:

echo 'Other error :