Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Shopify API to create product in PHP

Tags:

php

curl

shopify

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"}}
like image 844
Pundit Avatar asked Feb 05 '15 07:02

Pundit


People also ask

Can I use PHP with Shopify?

Building Your Own Shopify App with PHPFor custom apps, you can only install the app to one Shopify store with no need for Shopify to review your app. However, with public apps, Shopify will need to review your app in order for Shopify stores to install your Shopify app.

Can you integrate API into Shopify?

For example, you can use an API to connect your Shopify store to a shipping service, so that orders are automatically sent to the shipping service when they are placed on your store. PRO TIP: If you are considering integrating an API to your Shopify account, be aware that there are potential risks involved.


1 Answers

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

like image 176
Josh Brown Avatar answered Oct 01 '22 16:10

Josh Brown