在这里,我坚持通过API
创建webhook我正在使用cake php创建公共shopify应用程序
现在我想使用API为我的应用创建购物车/更新挂钩这里是我的代码 Cake 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->设置 - >通知创建webhook,则会在此处显示列表https://test.myshopify.com/admin/webhooks.json
请告诉我们如何使用API(cake php)创建webhook
答案 0 :(得分:1)
Shopify通过webhooks.json显示webhooks列表,这些是通过shopify管理员手动创建的。如果你想获得通过api创建的webhook列表,那么你需要从另一个浏览器或私人浏览器运行它(其中shopify admin没有插入)
您的链接将是这样的 - https://api-key:api-password@shop-name.myshopify.com/admin/webhooks.json
注意:替换应用的api密钥和密码,并替换链接中的商店名称,并在新的/私人浏览器窗口中进行尝试。