简体   繁体   中英

create product by api in shopify

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: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>";
?>

It gives response as '{"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)"}'.any idea?

请检查您的私人应用程序中是否为产品设置了写入权限。

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

尝试“已发布”=> false ,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM