Adding product images using CURL in PHP - Cloudflare errors

Hi,

I’m trying to add images to my products using CURL in PHP.

I’ve tried a number of different format but keep getting Cloudflare errors.

$imageApiUrl = "https://STORENAME.myshopify.com/admin/api/2022-07/products/PRODUCTID/images.json";
$newImageUrl = "PUBLIC PATH TO SHOPIFY PRODUCT IMAGE";
$ch = curl_init($imageApiUrl);
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, $newImageUrl);
curl_setopt($ch, CURLOPT_INFILE, $newImageUrl);
curl_setopt($ch, CURLOPT_POSTFIELDS,'{"image":{"position":'.$x.',"src":"'.$newImageUrl.'"}}');
$responseTmp = curl_exec($ch);

I’m getting “BAD REQUEST” and “1020” errors from Cloudflare, I assume because something in my request isn’t formatted correctly.

Would appreciate any advice.

Thanks

Answered my own question - in case it helps someone further down the track:

$image = json_encode(array('image'=> array('src' => $newImageUrl)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $image);
1 Like