Create A Webhook Using API In Shopify

- 1 answer

Here I stuck in shopify creating webhook through API

I am using cake php for creating public shopify app

Now I would like to create carts/update hook for my app using API here is my code Cake php library :

File : ShopifyApiComponent.php


public function createwebhook($shop_domain, $access_token){

    $method = "POST";
    $path = "/admin/webhooks.json";

    $params = array("webhook" => array( "topic"=>"carts/create",
                    "address"=>  $this->site_url."users/upUpdateCart",
                    "format"=> "json"));

    $password = md5($this->secret.$access_token);//If your shopify app is public
    $baseurl = "https://".$this->api_key.":".$password."@".$shop_domain."/";

    $url = $baseurl.ltrim($path, '/');
    $query = in_array($method, array('GET','DELETE')) ? $params : array();
    $payload = in_array($method, array('POST','PUT')) ? stripslashes(json_encode($params)) : array();
    $request_headers = in_array($method, array('POST','PUT')) ? array("Content-Type: application/json; charset=utf-8", 'Expect:') : array();
    $request_headers[] = 'X-Shopify-Access-Token: ' . $access_token;
                list($response_body, $response_headers) = $this->Curl->HttpRequest($method, $url, $query, $payload, $request_headers);

    $this->last_response_headers = $response_headers;
    $response = json_decode($response_body, true);

    if (isset($response['errors']) or ($this->last_response_headers['http_status_code'] >= 400))
        $body = $response['errors'];
        $body = $response_body;
    /*Debug the output in a text_file*/
    $destination = realpath('../../app/webroot/execution_log') . '/';
    $fh = fopen($destination."shopify_app.txt",'a') or die("can't open file");

    fwrite($fh, "\n\nDATE: ".date("Y-m-d H:i:s")."\n".$body);
    /*Debug Code Ends*/
    return (is_array($response) and (count($response) > 0)) ? array_shift($response) : $response;

and I called this function when I visit my app dashboard mean Controller : Offers function :dashboard

But its not seems to work because when I visit its showing nothing but If I am creating webhook through Admin->Setting->Notification then it show listing here

Please let me know how we can create webhook using API (cake php )



Shopify shows the list of webhooks through webhooks.json, those are created manually from the shopify admin. If you want to get the list of webhooks created through api then you need to run it from another browser or from a private browser (where shopify admin is not loged in)

Your link will be something like this - https://api-key:[email protected]/admin/webhooks.json

Note: replace api key and password of your app and replace shop name in the link and try it in a new/private browser window.