I want to create product in shopify through api.I tried with below code,but its not working.
<?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:[email protected]/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>";
?>
It gives response as ‘{“errors”:”[API] Invalid API key or access token (unrecognized login or wrong password)”}’.any idea?
3
Answers
Please check if there is write permission is set or not for products in your private app.
I have found the issue, it’s not about permission, is about the URL of the API you are posting it wrong URL. Here is the right one:
https://{apikey}:{password}@{hostname}/admin/api/{version}/{resource}.json
And here is the code:
try "published" => false ,