我一直在試圖將捲曲圖像添加到我的所有產品中,但都沒有成功。 我的圖片位於www.myshop.com/prestashop/img/ProductImages(我已經在這裏上傳了)。使用CURL自動添加產品圖像php
對於產品的每個產品我(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
它只是沒有做任何事情。 BVengerov指出了一個錯字,我會糾正它,並在這裏公佈結果(我之前沒有捕獲過這個結果,而且也失敗了)。 – Carollour
好吧,我糾正了這個錯誤,但是仍然沒有任何反應......我得到一個空白頁 – Carollour