php - Shopify API - 注册 webhook 时出现 822 错误
问题描述
嗨,我正在尝试像这样注册 Shopify webhook
/**
* @param $webHook
* @return array
*/
public function registerWebHook($webHook)
{
try {
$request = $this->request('admin/webhooks.json', [
'form_params' => [
'webhook' => [
'topic' => $webHook,
'address' => trim(config('app.url'), '/') . '/webhooks',
'format' => 'json'
]
]
], 'post');
} catch(BadResponseException $e) {
dd($e->getResponse()->getBody()->getContents());
}
return $this->parseResponseBody($request);
}
底层代码设置访问令牌标头等,所有其他请求都可以正常工作,只是这个返回以下错误;
{"error":"822: unexpected token at 'webhook%5Btopic%5D=products%2Fupdate\u0026webhook%5Baddress%5D=https%3A%2F%2F51t7e3cb.ngrok.io%2Fwebhooks\u0026webhook%5Bformat%5D=json'"}
任何帮助表示赞赏。
编辑
/**
* @param string $endpoint
* @param array $payload
* @param string $method
* @return \Psr\Http\Message\ResponseInterface
*/
public function request($endpoint = '', $payload = [], $method = 'get')
{
$payload['headers'] = [
'X-Shopify-Access-Token' => session()->get('access_data')['access_token'],
'Content-Type' => 'application/json'
];
$client = new \GuzzleHttp\Client();
$url = sprintf(
'https://%s/%s',
env('SHOPIFY_SHOP_DOMAIN'),
$endpoint
);
$response = $client->{$method}($url, $payload);
if($response->getStatusCode() == 200) {
return $response;
}
}
解决方案
您说您是application/json
通过请求标头发送的,但是使用form_params
有效负载数组中的键会导致数据格式化为application/x-www-form-urlencoded
.
这应该使用该位置的json
密钥,以便 guzzle 知道您希望将其编码为请求正文中的 JSON。http://docs.guzzlephp.org/en/stable/request-options.html#json
推荐阅读
- php - 通过 PHP (user-id)/picture 获取 Facebook 头像
- docker - 超级账本结构对等变量
- python - 如何从 tf.nn.embedding_lookup() 修改返回张量?
- ruby-on-rails - Solidus - 第一次安装,尝试运行 spree 时出错:安装生成器
- ffmpeg - FFMPEG:基于 PTS/DTS 删除数据包
- kubernetes - 拒绝连接到 Kubernetes 服务
- excel - VBA 在范围内使用 right()
- google-app-engine - App Engine 在现有项目上部署错误 500,而不是在新项目上
- javascript - 使用 .env 文件进行单元测试
- video - 如何裁剪视频底部然后删除黑色边框?