我正在尝试按照https://help.shopify.com/api/reference/product_image#create中列出的示例,特别是这一个: 使用将由Shopify
下载的源URL创建新的产品图像POST /admin/products/#{id}/images.json
{
"image": {
"src": "http:\/\/example.com\/rails_logo.gif"
}
}
然而,当我尝试它时,我从shopify获得错误代码406(无效请求)。我修改了这段代码的几部分,但这是我的最新版本:
#Add the product image
$thisURL = "https://".$APIKeys[$channel_id].":".$shopifyPwds[$channel_id]."@".$shopify_subdomain.".myshopify.com/admin/products/$shopify_product_id/images.json";
$ch = curl_init($thisURL);
$image = array(
"src" => $imageURL
);
$data_string = json_encode(array('image' => $image));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Content-Length: ' . strlen($data_string)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
if ($debug) {
echo "***About to send a request to $thisURL\n\n";
}
$output = curl_exec($ch);
if($output === false) {
$errorMsg="Sent the following to $thisURL: ".$data_string." \n";
$errorMsg.='Curl error: ' . curl_error($ch);
if ($debug) {
echo "***".$errorMsg."\n\n\n";
}
recordError(errorMsg);
} else {
if ($debug) {
echo "***Output from curl image upload was $output\n\n\n";
}
//Update the database
$outObj = '';
$outObj = json_decode($output);
$shopify_image_id = $outObj->image->id;
$sql = "insert into product_image_channels set product_image_shopify_id='".$shopify_image_id."',
product_image_id='".$imageRow['product_image_id']."', channel_id='".$channel_id."'
on duplicate key update product_image_id='".$imageRow['product_image_id']."', channel_id='".$channel_id."'";
execQuery($sql);
}
有人能看到我错过的东西吗?我希望有一个详细的消息,附带shopify错误代码。
更新:我可以通过创建一个与shopify通信的简单功能和一个用于测试不同api调用的简单表单来添加图像。功能如下:
//Send a command to shopify
function sendShopifyCmd($url,$httpAction,$data) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpAction);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
if (!empty($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1); // RETURN THE CONTENTS OF THE CALL
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
$output = curl_exec($ch);
if($output === false) {
$outObj= new stdClass();
$outObj->errorMsg="Sent the following to $url: \n".print_r($data,true)."\n";
$outObj->errorMsg.='Curl error: ' . curl_error($ch);
recordError($outObj->errorMsg);
return $outObj;
} else {
//return the output as an object
$outObj = json_decode($output);
return $outObj;
}
}
我仍然不知道为什么我之前无法添加图像,但是通过较短的反馈循环,我能够使其正常工作。原始代码在cron作业中运行,该作业正在检查要更新的“待定”产品,因此所需的实际测试流程不利于开发速度。有时需要不同的背景来使事情发挥作用。至于cron作业,我用函数调用替换了我原来的大部分代码,这也简化了它。
答案 0 :(得分:0)
我能够使用您问题中的代码创建产品图片。我唯一独有的是图片网址(在一些随机网站上使用图片)和Shopify管理网址。
再看看这些: