我正在尝试在shopify中构建一个应用程序,以便使用他们的API, as shown here 但是,当我在该URL上请求POST请求时,即
https://demo-store.myshopify.com/admin/script_tags.json
HTTP状态码400(错误请求)
返回错误{"errors":{"script_tag":"expected String to be a Hash"}}
示例代码
$access_token = "my-access-token";
$client = new \GuzzleHttp\Client();
try {
$response = $client->request($type, $url,[
'headers' => ['X-Shopify-Access-Token' => $access_token],
'form_params' => ['script_tag'=> \GuzzleHttp\json_encode(["event" => "onload", "src" => "https://app.dev/app.js"])],
]);
$result = json_decode($response->getBody()->getContents(), true);
var_dump($response, $result);
} catch (\Exception $ex) {
var_dump($ex);
}
答案 0 :(得分:0)
在向Shopify的API发出POST或PUT请求时,您通常需要Content-Type: application/json
标头。 Guzzle第6版has a json
option that adds the header automatically.
以下是创建脚本标记时的示例:
$client = new \GuzzleHttp\Client();
$script_tag = [
'script_tag' => [
'src' => 'https://sdks.shopifycdn.com/js-buy-sdk/v0/latest/shopify-buy.umd.polyfilled.min.js',
'event' => 'onload'
]
];
$response = $client->request('POST', 'https://45345345345.myshopify.com/admin/script_tags.json', [
'json' => $script_tag,
'headers' => ['X-Shopify-Access-Token' => $access_token]
]);