7

Thanks a lot for your solutions. I am using Shopify API along with PHP CURL POST Method to create product. The following is the code and output.

$products_array = array(
    "product"=>array(
        'title'=>'',
        "title"=> "Burton Custom Freestlye 151",
        "body_html"=> "<strong>Good snowboard!</strong>",
        "vendor"=> "Burton",
        "product_type"=> "Snowboard",
        "published"=> false ,
        "variants"=>array(
                        array(
                        "sku"=>"t_009",
                        "price"=>20.00,
                        "grams"=>200,
                        "taxable"=>false,
                        )
        )
    )
);
echo json_encode($products_array);
echo "<br />";
$url = "https://apikey:password@hostname/admin/products.json";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json'));
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_POSTFIELDS, json_encode($products_array));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec ($curl);
curl_close ($curl);
echo "<pre>";
print_r($response); 

Where I am using my credentials given in $url part. I am getting following error

{"product":{"title":"Burton Custom Freestlye 151","body_html":"Good snowboard!<\/strong>","vendor":"Burton","product_type":"Snowboard","published":false,"variants":[{"sku":"t_009","price":20,"grams":200,"taxable":false}]}}

HTTP/1.1 400 Bad Request
Server: nginx
Date: Thu, 05 Feb 2015 07:28:58 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Sorting-Hat-PodId: 0
X-Sorting-Hat-ShopId: 7628323
X-Sorting-Hat-PodId-Cached: 0
X-Sorting-Hat-ShopId-Cached: 0
Status: 400 Bad Request
X-XSS-Protection: 1; mode=block; report=/xss-report/2dfd1c1e-6c9c-4024-a9bf-f3f6b177eb17?source%5Baction%5D=create&source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin
X-Content-Type-Options: nosniff
X-ShopId: 7628323
X-ShardId: 0
X-Shopify-Shop-Api-Call-Limit: 1/40
HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT: 1/40
X-Stats-UserId: 0
X-Stats-ApiClientId: 619259
X-Stats-ApiPermissionId: 9949639
X-Request-Id: 2dfd1c1e-6c9c-4024-a9bf-f3f6b177eb17
X-Kafka-Logged: 1

{"errors":{"product":"Required parameter missing or invalid"}}

1 Answer 1

7

Try changing Accept: application/json to Content-Type: application/json

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for this small little bug. I will take care of such next time. Cheers!!
Bundles of thank both of you "Josh Brown" and "Pundit".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.