在 shopify 中使用 API 创建 webhook

Create a webhook using API in shopify

这里我卡在 shopify 通过 API 创建 webhook

我正在使用蛋糕 php 创建 public shopify 应用程序

现在我想使用 API 为我的应用程序创建 carts/update 挂钩这是我的代码 蛋糕 php 图书馆:https://github.com/cmcdonaldca/CakePHP-Shopify-Plugin

文件: 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'];
    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;
}

我在访问我的应用仪表盘时调用了这个函数意味着 控制器:报价 功能:仪表板

但它似乎不起作用,因为当我访问 https://test.myshopify.com/admin/webhooks.json 它只显示 如果我通过 Admin->Setting->Notification 创建 webhook,那么它会在此处显示列表 https://test.myshopify.com/admin/webhooks.json

请告诉我如何使用 API(蛋糕 php)创建 webhook

Shopify 通过 webhooks.json 显示 webhooks 列表,这些是从 shopify 管理员手动创建的。如果您想获取通过 api 创建的 webhooks 列表,那么您需要从另一个浏览器或私人浏览器(未登录 shopify 管理员)运行

您的 link 将是这样的 - https://api-key:api-password@shop-name.myshopify.com/admin/webhooks.json

注意:替换 api 应用程序的密钥和密码并替换 link 中的商店名称,然后在 new/private 浏览器中尝试 window。