Ad

How Can I Add An Image To Shopify Product Using The Product Image Api?

- 1 answer

I'm trying to follow the example listed at https://help.shopify.com/api/reference/product_image#create, specifically this one: Create a new product image using a source URL that will be downloaded by Shopify

POST /admin/products/#{id}/images.json
{
  "image": {
    "src": "http:\/\/example.com\/rails_logo.gif"
  }
}

However when I try it I get error code 406 (invalid request) from shopify. I've modified several pieces of this code but this is my latest version:

        #Add the product image
        $thisURL = "https://".$APIKeys[$channel_id].":".$shopifyPwds[$channel_id]."@".$shopify_subdomain.".myshopify.com/admin/products/$shopify_product_id/images.json";

        $ch = curl_init($thisURL);

        $image = array(
                "src" => $imageURL
        );

        $data_string = json_encode(array('image' => $image)); 

        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
                'Content-Length: ' . strlen($data_string)));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);  
        curl_setopt($ch, CURLOPT_FAILONERROR, true);
        if ($debug) {
            echo "***About to send a request to $thisURL\n\n";
        }
        $output = curl_exec($ch);
        if($output === false) {
            $errorMsg="Sent the following to $thisURL: ".$data_string." \n";
            $errorMsg.='Curl error: ' . curl_error($ch);
            if ($debug) {
                echo "***".$errorMsg."\n\n\n";
            }
            recordError(errorMsg);
        } else {
            if ($debug) {
                echo "***Output from curl image upload was $output\n\n\n";
            }
            //Update the database
            $outObj = '';
            $outObj = json_decode($output);
            $shopify_image_id = $outObj->image->id;
            $sql = "insert into product_image_channels set product_image_shopify_id='".$shopify_image_id."',
                    product_image_id='".$imageRow['product_image_id']."', channel_id='".$channel_id."' 
                    on duplicate key update product_image_id='".$imageRow['product_image_id']."', channel_id='".$channel_id."'";
            execQ='".$imageRow['product_image_id']."', channel_id='".$channel_id."'";
            execQuery($sql);
        }

Can someone see what I'm missing? I wish there was a detailed message which came along with the shopify error codes.

Update: I was able to add images by creating a simple function for communicating with shopify and a simple form for testing different api calls. The function is below:

//Send a command to shopify
function sendShopifyCmd($url,$httpAction,$data) {
    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpAction);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    if (!empty($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);  // RETURN THE CONTENTS OF THE CALL
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
    $output = curl_exec($ch);
    if($output === false) {
        $outObj= new stdClass();
        $outObj->errorMsg="Sent the following to $url: \n".print_r($data,true)."\n";
        $outObj->errorMsg.='Curl error: ' . curl_error($ch);
        recordError($outObj->errorMsg);
        return $outObj;
    } else {
        //return the output as an object
        $outObj = json_decode($output);
        return $outObj;
    }       
}

I still don't know why I couldn't add an image before, but with a shorter feedback loop I was able to get it working. The original code was running in a cron job which was checking for "pending" products to update, so the actual test flow required was detrimental to development speed. Sometimes it takes a different context to get things working. As for the cron job I replaced much of my original code with a function call which simplified that too.

Ad

Answer

I was able to create a product image using the code in your question. The only thing unique to me is the image url (used an image on some random website) and the Shopify admin url.

Take another look at these:

  • $APIKeys[$channel_id]
  • $shopifyPwds[$channel_id]
  • $shopify_subdomain
  • $shopify_product_id
  • $imageURL
Ad
source: stackoverflow.com
Ad