Jump to content

Update image with webservice


p_strange

Recommended Posts

$urlImage = 'https://motovalley.sites-preprod.fr/api/images/products/'.$productFields->id.'/?ps_method=PUT';
echo $urlImage;
$key  = 'NW7B6QC685E4CMI3NUEG9B327C9RRHUK';


$image_url = $motos[1]->imageUrl;
$imgURL = urldecode($image_url);
$image_name = (stristr($imgURL,'?',true))?stristr($imgURL,'?',true):$imgURL;
$pos = strrpos($image_name,'/');
$image_name = substr($image_name,$pos+1);


/* Check the directory. Create if not exist*/
if (!file_exists('/tmp/img/')) mkdir('/tmp/img/', 0777, true);


/* Copy image to local temp*/
$ch = curl_init($image_url);
$fp = fopen('/tmp/img/'.$image_name, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);


/* Set the local image path*/
$image_path = '/tmp/img/'.$image_name;
//Here you set the path to the image you need to upload
$image_mime = 'image/jpg';

$args['image'] = new CurlFile($image_path, $image_mime);

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_URL, $urlImage);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $key.':');
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if (200 == $httpCode) {
    echo 'Product image was successfully updated.';
}


I have already add a image on my product but i want to update them, i just add the method PUT but is doesnt work.

Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...