Join us on the demo, while our product experts provide a detailed walkthrough of our enterprise platform.

Shopify Embedded App

Recently, I was working on an implementation to build an embedded app on Shopify with PHP. I realized that the 3rd party PHP SDK recommended by Shopify called…

Lucius Yu
Lucius Yu
June 02, 2015
2 min read

Recently, I was working on an implementation to build an embedded app on Shopify with PHP. I realized that the 3rd party PHP SDK recommended by Shopify called "phpish" does not support the feature to "PUT" assets into your shopify shop's theme. The link to "phpish" git repository can be found  here, I'd like to thank these guys for their great work, it saved me lots of time to get the implementation on the right track.

So if you want to do a PUT API call to Shopify web service, you can do it in raw PHP by customizing a CURL request, this is not very hard. But prior to make the Asset API call, first you need to retrieve the currently activated theme ID by calling the Shopify Theme API call, this part can be done easily with the help of phpish.

Then it is the time to customize your CURL request to do the PUT API call, remember phpish stores our information such as Oauth Token and Shop into the $_SESSION, we can leverage on that or manually fill in the correct information

<!-- array("key" => "snippets/put-asset.liquid", "value" => "this is a test to put assets"));
$ch = curl_init($_SESSION['shop']."/admin/themes/$theme_id/assets.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$headers = array();
$headers[] = "X-Shopify-Access-Token: ".echo $_SESSION['oauth_token'];
$headers[] = "Content-Type: application/json";
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
echo "";
echo curl_error($ch)."";
var_dump($response);
curl_close($ch);
?>-->

Let me briefly explain this snippet, so first you define an array with key and value, key maps to the file name that you want to create, and value is the actual code or asset you want to pass in. Then you create a curl request with method "PUT" like all the other requests

$ch = curl_init($_SESSION['shop']."/admin/themes/$theme_id/assets.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

Next is something special for Shopify, to make an API call to Shopify you have to specify Oauth_Token in your request to tell Shopify who you are, and so does Content-Type.

$headers = array();
$headers[] = "X-Shopify-Access-Token: ".echo $_SESSION['oauth_token'];
$headers[] = "Content-Type: application/json";

Last but not least, json encode your data array into JSON format and send the request.

curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);

The last part is just to catch the returned response or show the error message from your CURL request if something is going wrong.

Hope this can help someone, happy coding.



LoginRadius Docs

Implement Authentication in Minutes

click here

Most Popular Tags

EngineeringJavaScriptNodeJsReactCSSSecurityOAuthAuthenticationGoSocialLogin

Are your customers safe on your application?

According to Forbes, data breaches exposed 4.1 billion records in the first six months of 2019. If this gets you worried, we’ve got your back!

LoginRadius protects your customers’ identities. We provide world-class security for your customers during login, registration, password setup, and any other data touchpoints, and make sure that their data is safe. We do so through by offering open source SDKs, integrations with over 150 third party applications, pre-designed and customizable login interfaces, and best-in-class data security products such as MFA, RBA, and Advanced Password Policies. The platform is already loved by over 3,000 businesses with a monthly reach of 1.17 billion users worldwide.Secure Your Application Now

Lucius Yu

Lucius Yu

Lucius is a Software Developer at LoginRadius. He was born and raised in China, but came to Edmonton to graduate from the University of Alberta, double majoring in Computer Sciences and Mathematics. Besides coding, which is his real passion, he enjoys playing ping pong and swimming.

View Profile

Try a Modern Authentication Solution

$0/ month

Free Sign Up
  • 7,000 Monthly Active Users
  • 1 Web or Mobile App
  • Email/Password Login
  • 3 Social Login Providers
  • User Data Storage
  • Transactional Email Template
  • Customizable Login Interfaces