If you are new to using the shopify API, you have landed to the right place. This will be a series of posts that explains the implementation of different shopify API in PHP and this post specifically describes retrieving the list of shopify product through the API in PHP.
- You need to enable curl (if it not enabled by default). To check if curl is enabled or not simply execute below script:
Now search for curl. If it is enabled you will see the enabled status.
- To make a read/write request on any shopify store through API you need the access to do so. So the first step of accessing the shopify store data is to create the <b>private app</b> that provide you with the API keys and access token using which you can do shopify API request.
- From your shopify admin , go to Apps.
- Click Manage private apps.
- Click Create a new private app.
- In the App details section, enter a name for your private app and a contact email address. …
- In the Admin API section, select the areas of your store that you want the app to be able to access.
Here you need to note the webhook API version, this will be used for API access (This is the recent feature added by shopify. If you want to learn more about this check out here )
- Save the App.
Now you will see API Key and password as shown on screenshot below. You will use these keys to do the API call to shopify
After these basic configuration,we are ready to to code.
Below code show the API request to retrieve the product list
//define your API key
$SHOPIFY_APP_API_KEY = 'YOUR_API_KEY';
$SHOPIFY_APP_PASSWORD = 'YOUR_API_PASSWORD';
$SHOPIFY_SHOP = 'YOUR_SHOP_URL'; //For eg: storedenavin.myshopify.com
$request = curl_init($requestUrl); // initiate curl object
curl_setopt($request, CURLOPT_HEADER, 0);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($request, CURLOPT_HTTPHEADER, ['Content-Type: application/json','Accept:*/*']);
curl_setopt($request, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($request, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($request); // execute curl fetch and store results in $response
In above code replace the value of the variable $SHOPIFY_APP_API_KEY , $SHOPIFY_APP_PASSWORD and $SHOPIFY_SHOP .
The requestUrl contains the webhook API version that you noted earlier.
Other is the basic curl request and as a response shopify returns list of shopify product on variable $json_data.