【发布时间】:2017-11-16 00:00:38
【问题描述】:
我正在尝试遵循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 工作,我用函数调用替换了大部分原始代码,这也简化了。
【问题讨论】:
标签: php image api curl shopify