0

I'm currently trying to add flickr api to my custom wordpress 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 ) {

        $this->api_key_shown_here = $flickr_key;
    }

    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';

        // 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( $flickr_key );

$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. ;)

  • Add debugging information. This is almost certainly a simple syntax error. I don't see the WordPress component though. The question looks off-topic to me. – s_ha_dum Oct 17 '13 at 14:00
  • Yep, you're not even using wp_remote_get. Break it in steps: is the URL valid? does it return a valid response? and so on... Oh, here's a WP/FlickAPI example. – brasofilo Oct 17 '13 at 14:54
  • @brasofilo, please view my edited version... I'm now able to see my site's page, but I'm still not seeing any images... – Designer 17 Oct 17 '13 at 19:39
1

I see two issues right off the bat that are going to cause problems:

$this->myApiKey = $flickr_key;
$thix->myApiSecret = $flickr_secret;
  1. myApiKey and myApiSecret aren't in your code anywhere, so setting them here isn't doing anything. They should be $this->flickr_key and $this->flickr_secret.
  2. You have a typo in the 2nd line above. Change $thix to $this and give it a go.
  • And I agree with @s_ha_dum that this is a general programming question and not a WordPress question (even though the code is in a plugin, it's not really WP-related). If there's a larger plugin question here, show the context that it's used within WP. – Morgan Estes Oct 17 '13 at 14:44
  • If you're saying that 'filckr_ket' and 'flickr_secret' are supposed to be you api key/secret number/letter combo. they are but I didn't want to show them. ;) Tried changing '$thix->' to '$this->' and still no good. – Designer 17 Oct 17 '13 at 15:08
  • I'm saying that when you use the $this->myApiKey assignment in your constructor, you're setting class variable named $myApiKey, which doesn't exist. The variable $flickr_key does exist, and is used in the searchPhotos() function. You need to change the code to set the proper var names in the constructor, and pass the proper data when you instantiate the class, e.g. new Flickr('your api key string', 'your api secret string'). – Morgan Estes Oct 17 '13 at 17:59
  • Please read the edited version of my post... I'm now able to see my site's page, but still don't see any images... any ideas would help to get me going in the right direction. Thanks! – Designer 17 Oct 17 '13 at 19:41
  • Okay, you're still missing the point about setting variables inside a class. The line $this->api_key_shown_here = $flickr_key; should be $this->flickr_key = $flickr_key. Take a look at php.net/manual/en/language.oop5.basic.php for an explanation of how this works inside a class. – Morgan Estes Oct 17 '13 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.