【发布时间】: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