我想通过api在shopify中创建产品。我尝试使用以下代码,但无法正常工作。
<?php
$products_array = array(
"product" => array(
"title" => "Test Product",
"body_html" => "<strong>Description!</strong>",
"vendor" => "DC",
"product_type" => "Test",
"published" => true ,
"variants" => array(
array(
"sku" => "t_009",
"price" => 20.00,
"grams" => 200,
"taxable" => false,
)
)
)
);
$SHOPIFY_API = "https://apikey:password@domainname.myshopify.com/admin/products.json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $SHOPIFY_API);
$headers = array( "Authorization: Basic ".base64_encode("apikey:password"),
"Content-Type: application/json",
"charset: utf-8");
curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($products_array));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec ($curl);
curl_close ($curl);
echo "<pre>";
print_r($response);
echo "</pre>";
?>
它以'{“ errors ::” [API]无效的API密钥或访问令牌(无法识别的登录名或错误的密码)“}}给出响应。任何想法吗?
答案 0 :(得分:0)
请检查您的私人应用中的产品是否设置了写入权限。
答案 1 :(得分:0)
我发现了问题,这与许可无关,与您向其发布错误API的API的URL有关。这是正确的一个:
https:// {apikey}:{password} @ {hostname} / admin / api / {version} / {resource} .json
这是代码:
<?php
$products_array = array(
"product" => array(
"title" => "New Test Product",
"body_html" => "<strong>Description!</strong>",
"vendor" => "DC",
"product_type" => "Test",
"published" => true ,
"variants" => array(
array(
"sku" => "t_009",
"price" => 20.00,
"grams" => 200,
"taxable" => false,
)
)
)
);
$API_KEY = 'apikey';
$PASSWORD = 'password';
$SHOP_URL = 'domainname.myshopify.com';
$SHOPIFY_API = "https://$API_KEY:$PASSWORD@$SHOP_URL/admin/api/2020-04/products.json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $SHOPIFY_API);
$headers = array(
"Authorization: Basic ".base64_encode("$API_KEY:$PASSWORD"),
"Content-Type: application/json",
"charset: utf-8"
);
curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($products_array));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec ($curl);
curl_close ($curl);
echo "<pre>";
print_r($response);
echo "</pre>";
?>