我正试图在PHP中使用CURL将图像添加到我的产品中,基于此:https://shopify.dev/api/admin-rest/2022-07/resources/product-image#post-产品产品id图像
这是使用CURL CLI时的样子,但我正试图通过PHP:使用它
curl -d '{"image":{"src":"http://example.com/rails_logo.gif"}}'
-X POST "https://your-development-store.myshopify.com/admin/api/2022-07/products/632910392/images.json"
-H "X-Shopify-Access-Token: {access_token}"
-H "Content-Type: application/json"
我尝试了许多不同的格式,但不断收到以下格式的Cloudflare错误:
$ch = curl_init("https://STORENAME.myshopify.com/admin/api/2022-07/products/632910392/images.json");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Shopify-Access-Token: SECRETTOKEN"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_URL, "http://example.com/rails_logo.gif");
curl_setopt($ch, CURLOPT_INFILE, "http://example.com/rails_logo.gif");
curl_setopt($ch, CURLOPT_POSTFIELDS,'{"image":{"position":'1',"src":"http://example.com/rails_logo.gif"}}');
$responseTmp = curl_exec($ch);
我得到了";坏请求";以及";1020〃;来自Cloudflare的错误,我想是因为我的请求中的某些内容格式不正确。
如有任何建议,不胜感激。
如果它能帮助其他人,这就是Shopify期望通过php curl:上传图像的方式
$image = json_encode(array('image'=> array('src' => $imageUrl)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $image);