Contentful logo

Contentful Community

Get image URL from asset within entry

I’m using the PHP SDK to retrieve content. Everything is connected well, as I’m able to retrieve entries of a specified type, loop through them, and echo out (for testing purposes) text fields within each entry.

However, one of my fields is a Media field (for an image), and I cannot seem to get anything to work as far as retrieving the image’s URL for display in the HTML. Most of my attempts seem to error out, as my output is halted at the first occurrence—but no errors are given in the console, so it’s hard for me to troubleshoot.

I was able to print_r the media field and see that it is a huge array which includes the file URL in there somewhere, but I can’t access it any way I try.

Here is my relevant code. Retrieving the teamName works just fine; primaryLogo is what errors out (I assume).

$query = new \Contentful\Delivery\Query();
$query->setContentType('fantasyTeam')
    ->orderBy('fields.teamId')
    ->setInclude(2);

$teams = $client->getEntries($query);
foreach ($teams as $team) {
    echo '<p>';
    echo $team->getTeamName() . '<br/>'; //this works fine
    echo $team->getPrimaryLogo(); //this doesn't work, nor do any variations I've tried
    // echo '<pre>' . print_r($team->getPrimaryLogo(), true) . '</pre>';
    echo '</p>';
}

Already answered in another thread: I don't understand the basic: how to get file url from entries? - #3 by Alma