2017-11-16 1 views
1

Ich versuche, das Beispiel bei https://help.shopify.com/api/reference/product_image#create aufgeführt zu folgen, und zwar diese: ein neues Produkt Bild erstellen Quell-URL verwenden, dieWie kann ich ein Bild hinzufügen, um ein Produkt mit dem Produktbild api zu laden?

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

jedoch von Shopify heruntergeladen wird, wenn ich es versuchen, erhalte ich Fehlercode 406 (ungültige Anfrage) von shopify. Ich habe einige Teile dieses Codes geändert, aber das ist meine neueste 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."'"; 
      execQuery($sql); 
     } 

Kann jemand sehen, was ich vermisse? Ich wünschte, es gäbe eine detaillierte Nachricht, die mit den Fehlercodes von shopify kam.

Update: Ich konnte Bilder hinzufügen, indem ich eine einfache Funktion zur Kommunikation mit shopify und ein einfaches Formular zum Testen verschiedener API-Aufrufe erstellte. Die Funktion ist unter:

//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; 
    }  
} 

Ich weiß immer noch nicht, warum ich nicht vor, ein Bild hinzufügen könnte, aber mit einer kürzeren Rückkopplungsschleife konnte ich es zum Laufen bringen. Der ursprüngliche Code wurde in einem Cron-Job ausgeführt, der nach "ausstehenden" Produkten suchte, die aktualisiert werden sollten. Daher war der tatsächliche Testfluss für die Entwicklungsgeschwindigkeit nachteilig. Manchmal braucht es einen anderen Kontext, um Dinge zum Laufen zu bringen. Was den Cron-Job angeht, habe ich viel von meinem ursprünglichen Code durch einen Funktionsaufruf ersetzt, der das ebenfalls vereinfacht hat.

Antwort

0

Ich konnte ein Produktbild mit dem Code in Ihrer Frage erstellen. Das einzige, was mir einzigartig ist, ist die Bild-URL (verwendet ein Bild auf einer beliebigen Website) und die Shopify-Admin-URL.

auf diese noch einen Blick:

  • $ APIKeys [channel_id $]
  • $ shopifyPwds
  • [channel_id $] $ shopify_subdomain
  • $ shopify_product_id
  • $ imageURL
+0

Danke für die Überprüfung @nicky! Ich war nicht in der Lage, diesen Code für mich arbeiten zu lassen, also erstellte ich eine einfache Funktion, die ich mit einem einfachen Formular zum Testen verschiedener API-Anrufe verwendete. Ich poste das als Update. – Robert

Verwandte Themen