skip to Main Content

I am building an app for shopify using PHP. I need to fetch current Theme’s Id and secondly i need to inject snippet. I am not sure about how to use Shopify Assests/Api. Can someone help me to solve these issues

2

Answers


  1. Chosen as BEST ANSWER

    Here is the Code i am using Please make me correct.

    <?php $url = 'https://' . $API_KEY . ':' . md5($SECRET . $TOKEN) . '@' . $shop . '/admin/themes/155035395/assets.json';
           $session = curl_init($url); 
            $asset = array('asset'=> array(
              'key' => 'snippets/newasset.liquid', 
              'value' => '{% comment %} here is your new snippet {% endcomment %}'
              ));
            curl_setopt($session, CURLOPT_HEADER, true);
            curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($session, CURLOPT_MAXREDIRS, 3);
            curl_setopt($session, CURLOPT_SSL_VERIFYPEER, true);
            curl_setopt($session, CURLOPT_SSL_VERIFYHOST, 2);
            curl_setopt($session, CURLOPT_CONNECTTIMEOUT, 30);
            curl_setopt($session, CURLOPT_TIMEOUT, 30);
            curl_setopt($session, CURLOPT_CUSTOMREQUEST, "PUT");
            //curl_setopt($session, CURLOPT_HTTPHEADER, ['Content-Type:     application/json']);
            curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json', 'X-Shopify-Access-Token: ' . $TOKEN));
            curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($asset));
        $result = curl_exec($session); 
             if(ereg("^(https)",$url))      curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
            $r = curl_getinfo($session);
            echo "<pre>";
            print_r($r);
    
            $result = curl_exec($session); 
            if ( curl_errno($session) ) {
                $result = 'cURL ERROR -> ' . curl_errno($session) . ': ' .     curl_error($session);
            } else {
                $returnCode = (int)curl_getinfo($session, CURLINFO_HTTP_CODE);
                switch($returnCode){
                    case 200:
                        break;
                    default:
                        $result = 'HTTP ERROR -> ' . $returnCode;
                        break;
                }
            }
            curl_close($session);
            echo $result;?>
    

  2. Shopify presents you with an endpoint labelled Theme. Use that to query for all the themes in a shop. You can check them for the one set as the main or current theme. Once you find the main, published theme, you can then use the ID of that theme to GET and POST assets, such as your snippet.

    As for not being sure about how to make secure Rest calls to the Shopify API, there are a million tutorials out there showing you how to do that. Shopify is the same as any other major web property in this respect. Nothing crazy to learn there but oAuth.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search