cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2: API REST insert image with PHP get error: Request does not match any route

SOLVED

Magento 2: API REST insert image with PHP get error: Request does not match any route

I am trying to add images to my new products or any product who doesn´t have one via API REST using PHP but I always received the error: Request does not match any route

 

Just after I create the product I call for this function... but the

 

     private function crearImagenes($productos) {
            
        $url = API_REST_URL; 
        $conImagen = $productos->imagen;
        
        $peticionNum = 0;
        $listaSKU = '';
        
        if ($conImagen === '1') {
            $urlImagen = $this->obtieneURLImagen($productos->skuCCE);
            $respuesta = true;
        } else {
            $urlImagen = NULL;
            $respuesta = false;
        }                  

        $cont=1;

        foreach ($urlImagen->data AS $imagen) {

            $contentType    = pathinfo($imagen, PATHINFO_EXTENSION);
            $content_Type   = 'Content-Type: image/'.$contentType;
            
            $xmlImage = DIR_IMAGEN.$productos->skuCCE.'/'.$imagen;
            $xmlImage = $this->curl_get_contents($xmlImage);
            $xmlImage = base64_encode($xmlImage);           

            $dataString = array(
                'entry' => array(
                    'id'        => $productos->idProducto,
                    'mediaType' => 'image',
                    'label'     => $productos->skuCCE,
                    'position'  => 0,
                    'disabled'  => 'false',
                    'extension_attributes'  => array(
                        'video_content' => array( )
                    ),
                    'types'     => array(
                                    'image',
                                    'thumbnail',
                                    'small_image' ),
                    'file'      => $productos->skuCCE,
                    'content'   => array(
                                    'base64_encoded_data'   => $xmlImage,
                                    'type'                  => 'image/'.$content_Type,
                                    'name'                  => $productos->skuCCE.$content_Type )
                )
            );

            $dataDecode = json_encode($dataString);
            
            $url .= "/products/".$productos->skuCCE."/media";

            $curl = curl_init();

            $token = "Authorization: Bearer ".$this->firmarUsuario();
            $headers = array(   $token,
                                "Accept: application/json",
                                "Cache-Control: no-cache",
                                $content_Type,
                                "User-Agent: Mozilla/4.0 (compatible)" );                     

            curl_setopt_array($curl, array(
                CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => $dataString,
                CURLOPT_HTTPHEADER => $headers,
              ));

            $response = curl_exec($curl);
            $responseDecode = json_decode($response);
            $err = curl_error($curl);

            curl_close($curl);

            if ($err) {
              echo "cURL Error #:" . $err;
            } else {
              echo $response;
            }

            $cont++;

            if ($cont >= 8) {
                break;
            }
        }  
        $flagMagento=$productos->bandera;
        $flagMagento[4]='1';
        $strConsulta="UPDATE tblProductos SET bandera='".$flagMagento."' WHERE tblProductos.codProd=".$productos->codProducto;

        $serSQL = new serverBD();
        $serSQL->MySQL();
        $serSQL->execute($strConsulta);            
        
        return $respuesta;
    }

 

Any help wil be appreciated. THANKS!

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Magento 2: API REST insert image with PHP get error: Request does not match any route

okay , i understood !!

 

you have issue with your type parameter of your product image parameters.

 

Basically its only accepts , like type => 'image/jpeg'; or whatever the extension of image.

 

Right now its taking , like 'type' => 'image/'.$content_Type, and Where $content_Type  = Content-Type: image/'.$contentType(extension of your image).

 

so currently full type parameter should be like this - type => image/image/png But we only needed like this type => 'image/jpeg'; And thats the actual issue.

 

So try below code to resolved this issue : 

 

 

 

$contentType = pathinfo($imagen, PATHINFO_EXTENSION);
//$content_Type = 'Content-Type: image/'.$contentType; // COMMENT this line its not needed

$dataString = array(
'entry' => array(
'id' => $productos->idProducto,
'mediaType' => 'image',
'label' => $productos->skuCCE,
'position' => 0,
'disabled' => 'false',
'extension_attributes' => array(
'video_content' => array( )
),
'types' => array(
'image',
'thumbnail',
'small_image' ),
'file' => $productos->skuCCE,
'content' => array(
'base64_encoded_data' => $xmlImage,
'type' => 'image/'.$contentType, // Here pass $contentType  varriable.
'name' => $productos->skuCCE.$content_Type )
)
);

 

 For More reference refer this link - https://magento.stackexchange.com/questions/180128/magento-2-how-to-upload-image-to-rest-api-post

if issue solved,Click Kudos & Accept as Solution

View solution in original post

5 REPLIES 5

Re: Magento 2: API REST insert image with PHP get error: Request does not match any route

You have issue with -  API_REST_URL

 

Check your final url , it should be like this - http://yourwebsiteurl/index.php/rest/V1/products/{sku}/media

 

Print your final media url and check with this.

if issue solved,Click Kudos & Accept as Solution

Re: Magento 2: API REST insert image with PHP get error: Request does not match any route

Hi Dave, thanks for your help

 

I already check the URL of the variable API_REST_URL and is like this:

define('API_REST_URL' , 'http://ccenet.mx/magento2/index.php/rest/V1');

Screen Shot 2018-02-12 at 09.20.10.jpg

 

 

and the moment I called it is here:

$url .= "/products/".$productos->skuCCE."/media";

Screen Shot 2018-02-12 at 09.44.10.jpg

 

and the result is this....Screen Shot 2018-02-12 at 09.44.32.jpg

 

 

I really don´t know how to fix this.

 

 

Re: Magento 2: API REST insert image with PHP get error: Request does not match any route

okay , i understood !!

 

you have issue with your type parameter of your product image parameters.

 

Basically its only accepts , like type => 'image/jpeg'; or whatever the extension of image.

 

Right now its taking , like 'type' => 'image/'.$content_Type, and Where $content_Type  = Content-Type: image/'.$contentType(extension of your image).

 

so currently full type parameter should be like this - type => image/image/png But we only needed like this type => 'image/jpeg'; And thats the actual issue.

 

So try below code to resolved this issue : 

 

 

 

$contentType = pathinfo($imagen, PATHINFO_EXTENSION);
//$content_Type = 'Content-Type: image/'.$contentType; // COMMENT this line its not needed

$dataString = array(
'entry' => array(
'id' => $productos->idProducto,
'mediaType' => 'image',
'label' => $productos->skuCCE,
'position' => 0,
'disabled' => 'false',
'extension_attributes' => array(
'video_content' => array( )
),
'types' => array(
'image',
'thumbnail',
'small_image' ),
'file' => $productos->skuCCE,
'content' => array(
'base64_encoded_data' => $xmlImage,
'type' => 'image/'.$contentType, // Here pass $contentType  varriable.
'name' => $productos->skuCCE.$content_Type )
)
);

 

 For More reference refer this link - https://magento.stackexchange.com/questions/180128/magento-2-how-to-upload-image-to-rest-api-post

if issue solved,Click Kudos & Accept as Solution

Re: Magento 2: API REST insert image with PHP get error: Request does not match any route

Dave,

 

Thank you for your help!!!!

Re: Magento 2: API REST insert image with PHP get error: Request does not match any route

@dgoldfeder

 

Glad that you have solved your issue , Happy to help and keep helping Smiley Happy

if issue solved,Click Kudos & Accept as Solution