0
votes

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
seems your API key is not valid - Always Sunny
Auth basic is not base64(usr:pass), its md5(md5(usr).':'.md5(relm).':'.md5(pass)), though looking at the docs, help.shopify.com/en/api/guides/… you can just use the https://apikey:password@ way and not send the basic auth header Authorization: Basic - Lawrence Cherone

3 Answers

0
votes

Please check if there is write permission is set or not for products in your private app.

0
votes

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:

<?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>";
?>
0
votes

try "published" => false ,