Jump to content

Webservice - get product image URLs


Recommended Posts

Hello,

i am able to pull all product infos via the webservice api out of the shop. However, I don't succeed to get the product's images frontend url. I only get the raw image data via /api/images/products/.

Is there any way to get the frontend urls of the product images? Something like https://my-shop.com/1-large_default/demo-artikel.jpg?

Thank you in advance!

Best
Alex

  • Like 1
Link to comment
Share on other sites

2 hours ago, Prestashop Addict said:

I think you can construct the url with the link_rewrite of the product, something like, not universal but should work

$uri = 'https://www.domai.ext/' . $id_productImage . '-large_default/' . $link_rewrite .'.jpg'

 

This solution works when writing PHP code inside prestashop. The good news is that the filename is ignored ($link_rewrite), so the image ID loaded from /api/images/products or /api/products is almost enough to build the public URL: https://www.domai.ext/{imageId}-large_default/whatever.jpg returns the correct image. The only guess is that the image type is jpg. Replacing whatever.jpg by whatever.png does not work.

Link to comment
Share on other sites

You should be able to retrieve link_rewrite with API in  product

    // Initialize the PrestaShop webservice
    $webService = new PrestaShopWebservice($shopUrl, $apiKey, false);

    // Retrieve product data (replace 1 with your product ID)
    $opt = array('resource' => 'products', 'id' => 1);
    $xml = $webService->get($opt);

    // Access product details
    $product = $xml->children()->children();

    // Get the link_rewrite field for the product
    $linkRewrite = (string)$product->link_rewrite;

    // Get the ID of the default image (cover image)
    $defaultImageId = (string)$product->associations->images->image[0]->id;

    // Construct the image URL
    $imageUrl = 'https://www.domai.ext/' . $defaultImageId . '-large_default/' . $linkRewrite .'.jpg';

and for file type you can use something like (poor performance 😞 but woks )

    // Possible file extensions
    $extensions = ['jpg', 'png', 'jpeg', 'webp'];

    // Function to check if a URL exists
    function url_exists($url) {
        $headers = get_headers($url);
        return stripos($headers[0], "200 OK") ? true : false;
    }

 

Link to comment
Share on other sites

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...