0

I am trying to access flickr images using api key and secret key. After searching it i got a zip file from the link . After giving apikey and secret key in the example.php file i m getting a Warning i.e

Warning: Invalid argument supplied for foreach() in C:\wamp\www\phpFlickr-3.1\example.php on line 21

Here is the example code which i m trying:

require_once("phpFlickr.php");
$f = new phpFlickr($api_key);

$recent = $f->photos_getRecent();

foreach ($recent['photo'] as $photo) {
  $owner = $f->people_getInfo($photo['owner']);
  echo "<a href='http://www.flickr.com/photos/" . $photo['owner'] . "/" . $photo['id']     . "/'>";
  echo $photo['title'];
echo "</a> Owner: ";
echo "<a href='http://www.flickr.com/people/" . $photo['owner'] . "/'>";
echo $owner['username'];
echo "</a><br>";
}

please help me i m trying to do this code from past 3 days but nothing works :(.

Simranjeet Kaur
  • 259
  • 1
  • 6
  • 18

2 Answers2

1

This piece of code can be used to test and confirm if the request-response between your app and Flickr app is proper. There are times when response from Flickr is not as expected due to incorrect request params, due to which the page will not render properly

Note : You may need to test if cURL is working on the server where you are planning to run this code, and do the changes necessary before you execute the below code.

<?php 

$url = 'https://api.flickr.com/services/rest/';
$data = array("method"=>"flickr.photos.getRecent","api_key"=>"{YOUR_API_KEY}","format"=>"json","nojsoncallback"=>"1");
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);

$result_data_json = json_decode($result,true);

$photoset = $result_data_json['photos'];
$photos = $photoset['photo'];

curl_close($ch);

foreach($photos as $pic){
    // https://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}.jpg
    $pic_thumbnail_url = 'https://farm'.$pic['farm'].'.staticflickr.com/'.$pic['server'].'/'.$pic['id'].'_'.$pic['secret'].'_n.jpg';
    $pic_url = 'https://farm'.$pic['farm'].'.staticflickr.com/'.$pic['server'].'/'.$pic['id'].'_'.$pic['secret'].'.jpg';
    $pic_large_url = 'https://farm'.$pic['farm'].'.staticflickr.com/'.$pic['server'].'/'.$pic['id'].'_'.$pic['secret'].'_b.jpg';

    echo '<img src='.$pic_thumbnail_url.'</img>';
 // echo '<br/><img src='.$pic_url.'</img>';
 // echo '<br/><img src='.$pic_large_url.'</img>';
}
?>
-1

You need an online server to try the code you are using since you want to test flick api. It is correct but you have to buy a server for a couple of hours just to try your code. A good suggestion is https://www.digitalocean.com

zeeks
  • 775
  • 2
  • 12
  • 30
  • Its not working yet.. Still getting the same Warning i.e Warning: Invalid argument supplied for foreach() in example.php on line 21 – Simranjeet Kaur Jul 04 '14 at 09:17