Create a webhook using API in shopify

生来就可爱ヽ(ⅴ<●) 提交于 2020-06-27 16:18:48

问题


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 : https://github.com/cmcdonaldca/CakePHP-Shopify-Plugin

File : ShopifyApiComponent.php

CODE :

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'];
    else
        $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");
    date_default_timezone_set('GMT');

    fwrite($fh, "\n\nDATE: ".date("Y-m-d H:i:s")."\n".$body);
    fclose($fh);
    /*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 https://test.myshopify.com/admin/webhooks.json its showing nothing but If I am creating webhook through Admin->Setting->Notification then it show listing here https://test.myshopify.com/admin/webhooks.json

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


回答1:


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:api-password@shop-name.myshopify.com/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.



来源:https://stackoverflow.com/questions/44495615/create-a-webhook-using-api-in-shopify

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!