在这里,我坚持使用shopify通过API创建webhook
我正在使用蛋糕PHP创建公共购物应用程序
现在,我想使用API为我的应用程序创建carts/update hook这里是我的代码蛋糕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;
}
当我访问我的应用程序dashboard时,我调用了这个函数mean Controller:Offers函数:dashboard
但它似乎不起作用,因为当我访问https://test.myshopify.com/admin/webhooks.json它没有显示任何东西,但如果我是通过管理员创建webhook-
请告诉我如何使用API(cake php)创建webhook
Shopify通过webhooks.json显示webhooks列表,这些是通过shop ify管理员手动创建的。如果你想获得通过api创建的webhooks列表,那么你需要从另一个浏览器或私人浏览器运行它(其中shop ify管理员没有登录)
您的链接将是这样的-https://api-key:api-password@shop-name.myshopify.com /admin/webhooks.json
注意:替换应用程序的api密钥和密码,替换链接中的店铺名称,然后在新的/专用浏览器窗口中尝试。