首页 > 解决方案 > 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;
        }
    }

标签: phpshopify

解决方案


您说您是application/json通过请求标头发送的,但是使用form_params有效负载数组中的键会导致数据格式化为application/x-www-form-urlencoded.

这应该使用该位置的json密钥,以便 guzzle 知道您希望将其编码为请求正文中的 JSON。http://docs.guzzlephp.org/en/stable/request-options.html#json


推荐阅读