0

I'm trying to add photo for existing product in my Prestashop 1.7.5.1 shop. I found example code on: https://devdocs.prestashop.com/1.7/development/webservice/tutorials/change_product_image/ and changed it so it has my shop addres and my key. I also changed line

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

$args['image'] = new CurlFile($image_path, $image_mime, '1.jpg'); beacause Visual Studio Code highlighted that CurlFile also needs third argument - postname. Here is my code:

<?php
    define('DEBUG', true);
    $url = 'http://test.my_shop.com';
    $key  = 'MY_AUTH_KEY';

    $psProductId = '2364';
    $urlImage = $url.'/api/images/products/'.$psProductId.'/';

    $image_path = '/img/1.jpg';
    $image_mime = 'image/jpg';
    $args['image'] = new CurlFile($image_path, $image_mime, '1.jpg');

    $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);

    print("<pre>");
    echo '<br>curl_exec <br>';
    $data_exec = curl_exec($ch);
    var_dump($data_exec);
    echo '<br>curl_getinfo <br>';
    var_dump(curl_getinfo($ch));
    echo '<br>curl_errno <br>';
    var_dump(curl_error($ch));
    echo '<br>curl_error <br>';
    var_dump(curl_error($ch));
    print("</pre>");

    curl_close($ch);
?>

I placed all those var_dumps because I wanted to gather as much info as possible. When I run this code in my browser using XAMPP i get following output:


curl_exec 
bool(false)

curl_getinfo 
array(37) {
  ["url"]=>
  string(48) "http://test.my_shop.com/api/images/products/2364/"
  ["content_type"]=>
  NULL
  ["http_code"]=>
  int(0)
  ["header_size"]=>
  int(0)
  ["request_size"]=>
  int(0)
  ["filetime"]=>
  int(-1)
  ["ssl_verify_result"]=>
  int(0)
  ["redirect_count"]=>
  int(0)
  ["total_time"]=>
  float(0.032204)
  ["namelookup_time"]=>
  float(0.001465)
  ["connect_time"]=>
  float(0.031879)
  ["pretransfer_time"]=>
  float(0)
  ["size_upload"]=>
  float(0)
  ["size_download"]=>
  float(0)
  ["speed_download"]=>
  float(0)
  ["speed_upload"]=>
  float(0)
  ["download_content_length"]=>
  float(-1)
  ["upload_content_length"]=>
  float(-1)
  ["starttransfer_time"]=>
  float(0)
  ["redirect_time"]=>
  float(0)
  ["redirect_url"]=>
  string(0) ""
  ["primary_ip"]=>
  string(15) "123.123.123.123"
  ["certinfo"]=>
  array(0) {
  }
  ["primary_port"]=>
  int(80)
  ["local_ip"]=>
  string(13) "192.168.0.106"
  ["local_port"]=>
  int(13956)
  ["http_version"]=>
  int(0)
  ["protocol"]=>
  int(1)
  ["ssl_verifyresult"]=>
  int(0)
  ["scheme"]=>
  string(4) "HTTP"
  ["appconnect_time_us"]=>
  int(0)
  ["connect_time_us"]=>
  int(31879)
  ["namelookup_time_us"]=>
  int(1465)
  ["pretransfer_time_us"]=>
  int(0)
  ["redirect_time_us"]=>
  int(0)
  ["starttransfer_time_us"]=>
  int(0)
  ["total_time_us"]=>
  int(32204)
}

curl_errno 
string(0) ""

curl_error 
string(0) ""

I have no idea what I'm doing wrong. I don't know why code posted on documentation page is not working dor me. If someone has any suggestion please post them, thanks.

piterz
  • 1

1 Answers1

0

I corrected first var_dump(curl_error($ch)); in my code and replaced it with var_dump(curl_errno($ch));. After i ran the code I got error no. 26

curl_errno
int(26)

I looked it up and found this answer: curl: (26) couldn't open file. I changed relative path to full path and now my code is fully working, photos are uploaded correctly. Here is my corrected code:

<?php
    define('DEBUG', true);
    $url = 'http://test.my_shop.com';
    $key  = 'MY_AUTH_KEY';

    $psProductId = '2364';
    $urlImage = $url.'/api/images/products/'.$psProductId.'/';

    $image_path = 'C:/xampp/htdocs/my_folder/img/1.jpg';
    $image_mime = 'image/jpg';
    $args['image'] = new CurlFile($image_path, $image_mime, '1.jpg');

    $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);

    print("<pre>");
    echo '<br>curl_exec <br>';
    $data_exec = curl_exec($ch);
    var_dump($data_exec);
    echo '<br>curl_getinfo <br>';
    var_dump(curl_getinfo($ch));
    echo '<br>curl_errno <br>';
    var_dump(curl_errno($ch));
    echo '<br>curl_error <br>';
    var_dump(curl_error($ch));
    print("</pre>");

    curl_close($ch);
?>
piterz
  • 1
  • It's too bad that `curl_error()` doesn't show that. I guess it only shows errors that come from the server, not local errors. – Barmar Sep 18 '19 at 15:19