我能够成功更新产品标题但是当我尝试购买api来更新我的产品图片时我会不断获取状态代码400错误请求...这里是我的代码以下...我会感谢任何帮助
public function alt(){
function __construct() {
parent::__construct();
$data = array(); //Always define $data in the constructor to make it available throughout your code
}
//getting shop info to access the app
$store_name = $this->session->userdata('store_name');
$this->load->database();
$this->load->model('Store');
$storeinfo_data=$this->Store->load_store_info($store_name);
$store_name_db = $storeinfo_data->store_name;
$store_name_tk = $storeinfo_data->access_token;
$store_name_url = $storeinfo_data->myshopify_domain;
$this->data['storeinfo']=$storeinfo_data;
if($store_name == $store_name_db){
//authenticating app with shopify api to be alble to access shop info
$sc = new ShopifyClient($store_name_url, $store_name_tk, SHOPIFY_KEY, SHOPIFY_SECRET);
//making a GET request to shopify api for shop products
$shop_products = $sc->call('GET', '/admin/products.json', null);
foreach($shop_products as $product) {
$product_id = $product['id'];
$product_d = $sc->call('GET', '/admin/products/' . $product_id . '/images.json', null);
echo "<pre>";
print_r($product_d);
$position = "1";
$key = "alt";
$value = "new alt tag content";
$metafields = array(
"key"=>"alt",
"value"=>"new alt tag content",
"value_type"=>"string",
"namespace"=>"tags"
);
$image = array(
"id"=> "16827152131",
"position" => 1,
"metafields"=>$metafields
);
//making a request to update image alt of a product in shop
try{
$shop_product = $sc->call('PUT','/admin/products/8445495491/images/16827152131.json', $image);
}catch( Exception $e ){
echo '<pre>';
print_r($e);
}
exit();
}
}else{
}
}
这是我得到的输出
ShopifyApiException Object
(
[method:protected] => PUT
[path:protected] => /admin/products/8445495491/images/16827152131.json
[params:protected] => Array
(
[id] => 16827152131
[position] => 1
[metafields] => Array
(
[key] => alt
[value] => new alt tag content
[value_type] => string
[namespace] => tags
)
)
[response_headers:protected] => Array
(
[http_status_code] => 400
[http_status_message] => Bad Request
[server] => nginx
[date] => Mon, 26 Sep 2016 09:11:59 GMT
[content-type] => application/json; charset=utf-8
[transfer-encoding] => chunked
[connection] => keep-alive
[x-frame-options] => DENY
[x-shopid] => 14690858
[x-shardid] => 2
[x-shopify-shop-api-call-limit] => 1/40
[http_x_shopify_shop_api_call_limit] => 1/40
[x-stats-userid] => 0
[x-stats-apiclientid] => 1423954
[x-stats-apipermissionid] => 31527902
[x-xss-protection] => 1; mode=block; report=/xss-report/9729121a-c8c6-4ff2-aee8-cb9963cd864f?source%5Baction%5D=update&source%5Bcontroller%5D=admin%2Fproduct_images&source%5Bsection%5D=admin
[x-request-id] => 9729121a-c8c6-4ff2-aee8-cb9963cd864f
[x-dc] => ash
[x-download-options] => noopen
[x-permitted-cross-domain-policies] => none
[x-content-type-options] => nosniff
)
[response:protected] => Array
(
[errors] => Array
(
[image] => Required parameter missing or invalid
)
)
[message:protected] => Bad Request
[string:Exception:private] =>
[code:protected] => 400
[file:protected] => /Applications/MAMP/htdocs/newp/application/libraries/shopify/shopify.class.php
[line:protected] => 70
[trace:Exception:private] => Array
(
[0] => Array
(
[file] => /Applications/MAMP/htdocs/newp/application/controllers/platforms/Shopify.php
[line] => 347
[function] => call
[class] => ShopifyClient
[type] => ->
[args] => Array
(
[0] => PUT
[1] => /admin/products/8445495491/images/16827152131.json
[2] => Array
(
[id] => 16827152131
[position] => 1
[metafields] => Array
(
[key] => alt
[value] => new alt tag content
[value_type] => string
[namespace] => tags
)
)
)
)
[1] => Array
(
[file] => /Applications/MAMP/htdocs/newp/system/core/CodeIgniter.php
[line] => 514
[function] => alt
[class] => Shopify
[type] => ->
[args] => Array
(
)
)
[2] => Array
(
[file] => /Applications/MAMP/htdocs/newp/index.php
[line] => 315
[args] => Array
(
[0] => /Applications/MAMP/htdocs/newp/system/core/CodeIgniter.php
)
[function] => require_once
)
)
[previous:Exception:private] =>
)
答案 0 :(得分:2)
我注意到了一些事情。图像数据必须包含在{"image": ... }
对象中,metafields
应该作为数组。此外,如果要修改现有元数据,则在有效负载中包含元数据ID。
尝试替换它:
$image = array(
"id"=> "16827152131",
"position" => 1,
"metafields"=>$metafields
);
有这样的事情:
$image = array("image" => array(
"id"=> 16827152131,
"position" => 1,
"metafields"=> $metafields
));
并替换它:
$metafields = array(
"key"=>"alt",
"value"=>"new alt tag content",
"value_type"=>"string",
"namespace"=>"tags"
);
有这样的事情:
$metafields = array(array(
"id"=> 123,
"key"=>"alt",
"value"=>"new alt tag content",
"value_type"=>"string",
"namespace"=>"tags"
));
123
将是您要修改的元字段的ID。您可以使用以下API调用获取该ID:GET /admin/metafields.json?metafield[owner_id]=16827152131&metafield[owner_resource]=product_image
您可以在此处找到正确格式的示例:https://help.shopify.com/api/reference/product_image#update
希望这有帮助!