How to fetch Media Assets of a Product from Akeneo 5

Viewed 197

I try to fetch the media assets from products in

I am using https://github.com/akeneo/api-php-client-ee (v6) and know about https://api.akeneo.com/api-reference-50.html

This is what I came up so far:

$searchBuilder = new \Akeneo\Pim\ApiClient\Search\SearchBuilder();
$searchBuilder->addFilter('enabled', '=', true);
$searchFilters = $searchBuilder->getFilters();

$products = $client->getProductApi()->all(100, ['search' => $searchFilters]);

foreach ($products as $product) {
    foreach($product['values']['assets'] as $assetData) {
    foreach($assetData['data'] as $code) {
        echo $code;
        $asset = $client->getProductMediaFileApi()->all();
        var_dump($asset);
    }
}

What I have tried / Questions:

  • I get a code like 1234_00 (if 1234 is the product number), but I do not know how to fetch the specific file from the product media file api. Do I have to filter here? How?

  • I tried to $client->getAssetMediaFileApi()->download($code) but the $code I have does not seem to be the full asset code (I get a 404 not found error)

How can I find out which assets are related to a specific product to download them or get the download URL?

1 Answers

This works - "bilder" is the Asset Family code in our case.

foreach ($products as $product) {
    foreach($product['values']['assets'] as $assetData) {
    foreach($assetData['data'] as $code) {
        echo $code;
        $assets = $client->getAssetManagerApi()->get('bilder', $code);
        foreach($assets['values']['media'] as $dataLine) {
            $download = $client->getAssetMediaFileApi()->download($dataLine['data']);
            file_put_contents('/tmp/' . basename($dataLine['data']), $download->getBody());
        }

        foreach($assets['values']['variation_image'] as $dataLine) {
            $download = $client->getAssetMediaFileApi()->download($dataLine['data']);
            file_put_contents('/tmp/' . basename($dataLine['data']), $download->getBody());
        }
    }

I found the main clue, by looking at the Akeneo Admin Panel and which requests it does :-)

Related