skip to Main Content

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


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

    Login or Signup to reply.
  2. 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>";
    ?>
    
    Login or Signup to reply.
  3. try "published" => false ,

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search