Search code examples
phpmagentomagento-soap-api

Magento catalogProductAttributeMediaCreate() exception


I'm uploading images to my Magento server using the v2 SOAP web services. My PHP code is:

// Session and other stuff...
//

$file = fopen ( "magento_art.txt" , "r" );
$data = fgetcsv ( $file , 2048, "$", "\\");

while (( $data = fgets ( $file , 2048)) !== false) {

    $data = str_replace('º', '&deg', $data);
    $data = str_replace('"', '', $data);
    $data = str_getcsv( $data, "$");

    $path= 'fotos/'.$data[14];
    $type = pathinfo($path, PATHINFO_EXTENSION);
    $raw_image = file_get_contents($path);
    $base64 = 'data:image/' . $type . ';base64,' . base64_encode($raw_image);
    $image = array(
        'content' => $base64,
        'mime' => 'image/jpeg'
    );

    var_dump($image);
    echo "\n\n";

    try{
        print_r("SKU: ".$data[1]."\n\n");
        $result = $client->catalogProductAttributeMediaCreate(
            $session,
            (string)$data[1],
            array('file' => $image, 'label' => 'image', 'position' => '100', 'types' => ['image'], 'exclude' => 0)
        );
    }

    catch(Exception $e){
        echo "Exception: ".$e->getMessage()."\n\n";
    }

And every single time I get the exception 'product not exists'. I've checked myself that the product exists in my server and the SKU code is the same in my PHP script and in my server, but the API method is not working.

¿What can it be? I've tried also 'hardcoding' a SKU of one of my products and that didn't work either.


Solution

  • you need to pass a last parameter, that said what is your indentifier.

    So your code should look like that:

    $result = $client->catalogProductAttributeMediaCreate(
                $session,
                (string)$data[1],
                array('file' => $image, 'label' => 'image', 'position' => '100', 'types' => ['image'], 'exclude' => 0),
                'sku'
            );