phpapiakeneo

How to fetch Media Assets of a Product from Akeneo 5


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:

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


Solution

  • 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 :-)