1

I'm currently trying to add flickr api to my custom plugin... and am failing. :( I was trying to use this tutorial, Getting Started With Flickr API, to learn the basic code structure and then I was going to refer to flickr's app garden for documentation to do what I'm wanting; but, it's not working.

What I have... I built a new PHP file and named it "class.flickr.php" and added the code listed in the tutorial...

<?php

class Flickr{

private $flickr_key;
private $flickr_secret;
private $format = 'json';

// Setting up flickr_key and flickr_secret
public function __construct( $flickr_key, $flickr_secret ) {

    $this->myApiKey = $flickr_key;
    $thix->myApiSecret = $flickr_secret;
}

public function searchPhotos( $query = '', $tags = '' ){ // Begin searchPhotos 

    $urlencoded_tags = array();

    if ( !empty( $args )) {

        $tags_r = explode( ',', $tags );
        foreach ( $tags_r as $tag ) {

            $urlencoded_tags[] = urlencode( $tag );
        }
    }

    // Construct the url
    $url  = 'http://api.flickr.com/services/rest/?';
    $url .= 'method=flickr.photos.search';
    $url .= '&text=' . urlencode( $query );
    $url .= '&tags=' . implode( ',', $urlencoded_tags );
    $url .= '&sort=relevance';
    $url .= '&safe_search=1';
    $url .= '&content_type=4';
    $url .= '&api_key=' . $this->$flickr_key;
    $url .= '&format' . $this->$format;
    $url .= '&per_page=10';

    // Calling url using curl
    $curl = curl_init();

    curl_setopt_array( $curl, array(

        CURLOPT_RETURNTRANSFER => 0,
        CURLOPT_TIMEOUT => 120,
        CURLOPT_URL => $url,            
    ));

    if ( !curl_exec( $curl )) {

        die ( 'Error: "' . curl_error( $curl ) . '" - Code: ' . curl_errno( $curl ));
    }

    print_r( curl_getinfo( $curl ));

    // Get search results
    $result = file_get_contents( $url );

    // Remove the unneccessary strings that wraps the result returned from the API
    $json = substr( $result, strlen( "jsonFlickrApi("), strlen( $result ) - strlen( "jsonFlickrApi(") - 1 );

    $photos = array();
    $data = json_decode( $json, true );

    // Check if the status didn't fail
    if ( $data['stat'] != 'fail' ) {

        // Return only the data for the photos as that's the only thing that we need
        $photos = $data['photos']['photo'];
        return $photos;
    } else {

        return false;       
    }
} // end searchPhotos

}

and then tried calling the above method in my page template (which is in the same folder as "class.flickr.php"), using the following code...

<?php // Flickr search photos test

    require_once( 'class.flickr.php' );

$flickr = new Flickr( $api_key, $api_secret);

$results = $flickr->searchPhotos( $query, $tags );
if ( !empty( $results )) {

    foreach( $results as $photo ) {

    $src = "http://farm" . $photo['farm'] . ".static.flickr.com/" . $photo['server'] . '/' . $photo['id'] . '_' . $photo['secret'] . '_m.jpg';
    ?>

    <img  src="<?php echo $src; ?>" alt="<?php echo $photo['title']; ?>" />

    <?php }                 
}

But, alas, all I'm getting is a blank screen. Any help would be appreciated. ;)

Designer 17
  • 283
  • 1
  • 4
  • 17

1 Answers1

0

This is something I wrote about in relation to some hosts becoming more restrictive in in file_get_contents calls lately. Basically, you need to use curl calls, and not just a raw file_get_contents.

Here is the answer I gave to solve this problem - It should help you in your situation (I included some sample code there): Wordpress simplexml_load_file() parse error

Community
  • 1
  • 1
edtheprogrammerguy
  • 5,957
  • 6
  • 28
  • 47
  • I actually did some more research and checked my error log and can now see my sites page by changing things like this '$flickr = new Flickr( $api_key, $api_secret);' to this '$flickr = new Flickr( api_key_shown_here );' and this '$url .= '&api_key=' . $this->$flickr_key; $url .= '&format' . $this->$format;' to this '$url .= '&api_key=' . $this->flickr_key; $url .= '&format' . $this->format;' among other things but I'm still not seeing any images... could that still be tied to the issue you mentioned in your answer? – Designer 17 Oct 17 '13 at 19:30
  • Okay, I've now edited my question to show how I'm using curl. I also discovered that I'm now getting an error from flickr 100: Invalid API Key (Key has invalid format). do you see any reason for this? – Designer 17 Oct 18 '13 at 00:23
  • I now have it retrieving image data from flickr with no link... which is why (I think) they're not being made visible on the screen. – Designer 17 Oct 18 '13 at 01:41
  • I acutally discovered that `cURL` is actually unneeded... It's embarassing, but here's the answer... [link](http://stackoverflow.com/questions/19452378/why-wont-the-images-that-are-grabbed-from-flickr-display) – Designer 17 Oct 18 '13 at 22:51