Jump to content

Array Access Problem


foucquet

Recommended Posts

I am trying to pull images from my Flickr account with some success except that of accessing the description to be used as a caption for the image. What I have so far is:-

 

<?php

    //>>> Define some variables
$maximgs = 5;
$images = array();

$params = array(
    'api_key' => '***********************',
    'method' => 'flickr.photos.search',
    'user_id' => '****************',
    'sort' => 'date-posted-desc',
    'format' => 'php_serial',
    'extras' => 'description',
    );

$encoded_params = array();

foreach ($params as $key => $value){
  $encoded_params[] = urlencode($key) . '=' . urlencode($value);
}

    //>>> Make request and decode the response
$url = 'http://api.flickr.com/services/rest/?' . implode('&', $encoded_params);

$response = file_get_contents($url);
$response_object = unserialize($response);

    //var_dump($response_object['photos']['photo']);

    //>>> Why is this returning 100 results?
if($response_object['stat'] !== 'ok'){
  echo "Something went wrong!";
}

    //>>> Select the required number of images ($maximgs)
for($loop = 0; $loop <= $maximgs; $loop++){
    $images = $response_object['photos']['photo'][$loop];

    var_dump($images);

    //>>> Url of the individual image location for the img src.
    $imgurl = 'http://farm'.$images['farm'].'.'.'static'.'.'.'flickr.com/'.
    $images['server'].'/'.$images['id'].'_'.$images['secret'].'_n.jpg';

    //>>> Url of the individual image for the link.
    $flkrurl = 'http://www.flickr.com/photos/'.$images['owner'].'/'.$images['id'].'/';

    /*The complete Url
    (<a href="link($flkrurl)">
    <img alt="$images['title']" src="image location"($imgurl) width="required width of image" />
    </a>) */
    $compurl = '<a href="'.$flkrurl.'" target="_blank">'.'<img alt="'.
    $images['title'].'" src="'.$imgurl.'" width="320px" /></a>';

    //>>> Display Images
    echo $compurl . '<br />' . $images['description']['content'];    //<<<<<< Line 66

}

?>

 

Which annoyingly throws this error:-

 

Notice: Undefined index: content in C:\wamp\www\php\flickr_test1.php on line 66

 

The result of a var_dump():-

 

array

  'id' => string '6914498038' (length=10)

  'owner' => string '************' (length=12)

  'secret' => string '601a885f31' (length=10)

  'server' => string '7137' (length=4)

  'farm' => float 8

  'title' => string 'newhaven_15' (length=11)

  'ispublic' => int 1

  'isfriend' => int 0

  'isfamily' => int 0

  'description' =>

  array

      '_content' => string 'Great reflections found in Newhaven Harbour.' (length=44)

 

with the info that I want to extract underlined. What I can't figure out is how to find out the name of the array below "description =>". I have been scratching my head for several hours now and am no nearer to working it out. Maybe a genius here will have the solution.

Link to comment
https://forums.phpfreaks.com/topic/261476-array-access-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.