在 shopify 中使用 API 创建 webhook [英] Create a webhook using API in shopify

查看:127
本文介绍了在 shopify 中使用 API 创建 webhook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我坚持通过 API 创建 webhook 的 shopify

Here I stuck in shopify creating webhook through API

我正在使用 cake php 创建公共 shopify 应用

I am using cake php for creating public shopify app

现在我想使用 API 为我的应用程序创建购物车/更新挂钩,这是我的代码蛋糕 php 库:https://github.com/cmcdonaldca/CakePHP-Shopify-Plugin

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

文件: ShopifyApiComponent.php

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'];
    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

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

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

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

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

推荐答案

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

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)

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

Your link will be something like this - https://api-key:api-password@shop-name.myshopify.com/admin/webhooks.json

注意:替换您应用的 api 密钥和密码并替换链接中的商店名称,然后在新的/私人浏览器窗口中尝试.

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.

这篇关于在 shopify 中使用 API 创建 webhook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆