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
Share on other sites

If the vardump has not been modified, I would guess this works:

$images['description']['_content']

^ See the extra underscore before content.

 

array

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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