2016-09-12 3 views
2

제품의 조합으로 이미지를 매핑하고 싶습니다. 코드 아래에 성공적으로 이미지를 추가 할 수 있지만 반환 결과에서 이미지 ID를 제공하지 않습니다.prestashop webservice에서 이미지 ID를 얻는 방법

코드 :

$img_path = DIR_PATH_CONFIG.'/'.$this->filename.'_images/'. $imagename; 
     //image will be associated with product id 4 
     $url = PS_SHOP_PATH. '/api/images/products/'.$id_product; 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_POST, true); 
     //curl_setopt($ch, CURLOPT_PUT, true); To edit a picture 
     curl_setopt($ch, CURLOPT_USERPWD, PS_WS_AUTH_KEY.':'); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, array('image'=>"@".$img_path.";type=image/jpeg")); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     $result = curl_exec($ch); 
     curl_close($ch); 
     if($result === false) 
     { 
     $var = false; 
     echo "<br><br>Error : ".curl_error($result)."<br>"; } 
     else 
     { 
     echo '<br><br> Image added'; 
     //$xml = simplexml_load_string($result); 
     //$imageId = $xml->image->id; 
     unlink($img_path); 
     $var = true; 
     } 

어느 한 가지는 생각은 알려 주시기 바랍니다 수 ...?

답변

0

이미지가없는 제품에 이미지를 추가하십시오 (/ api/images/products/{ID}에는 항목이 없습니다). 열쇠는 이미지의 바이너리를 PS WS로 다시 보내지 않고 새로운 CurlFile을 만드는 것입니다.

/** 
 
* Function that creates a new Product Feature Value and returns its newly created id 
 
* @param product_id = the id of the product for which the image should be uploaded 
 
* @param image_name = the String containing the name of the downloaded image (which will be found in /img) 
 
* @return the ID of the newly inserted image 
 
*/ 
 
function addNewImage($product_id, $image_name ) { 
 
    $url = PS_SHOP_PATH . '/api/images/products/' . $product_id; 
 
    $image_path = 'img/'. $image_name; 
 
    $key = PS_WS_AUTH_KEY; 
 

 
    $ch = curl_init(); 
 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:multipart/form-data','Expect:')); 
 
    curl_setopt($ch, CURLOPT_URL, $url); 
 
    curl_setopt($ch, CURLOPT_POST, true); 
 
    curl_setopt($ch, CURLOPT_USERPWD, $key.':'); 
 
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => new CurlFile($image_path))); 
 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
 

 
    $result = curl_exec($ch); 
 
    curl_close($ch); 
 

 
    return $result; 
 
}

다음과 같은 기능을 사용할 수 있습니다