1

I've encountered a rather weird problem that has me completely stumped.

I'm trying to pass a PHP variable from one file to another, then plug that variable into a search function within the Flickr API.

To put in context, it's pulling a variable ($location) containing the location of a weather forecast. $location (located in scraper.php), which is defined by user input, goes into the search function. The function sends a request to the Flickr API, which returns an image url.

as it goes:

require_once('flickr.php'); 
include 'scraper.php';

$Flickr = new Flickr; 
$data = $Flickr->search($location); 

Keep in mind, this all works beautifully if I define the search string in the file itself. It's only when I try to use a variable from scraper.php that it doesn't search for anything at all. For example:

$string = 'hawaii'
$Flickr = new Flickr; 
$data = $Flickr->search($string);

The above works just fine. To make things even more confusing, if I echo '$location' before it goes into the search function I get the proper result just fine. The variable is there and included. It just won't carry through to the function for some reason.

Any help would be appreciated. Here is the full code from the project:

search.php:

echo $location;
$Flickr = new Flickr; 
$data = $Flickr->search($location); 

foreach($data['photos']['photo'] as $photo) {  

    //returns 1 photo url at large size (1024)
    $image_url = 'http://farm' . $photo["farm"] . '.static.flickr.com/' . $photo["server"] . '/' . $photo["id"] . '_' . $photo["secret"] . '_b.jpg';
} 

flickr.php (api key is taken out):

include 'scraper.php';

class Flickr { 
    private $apiKey = 'MYAPIKEY'; 

    public function __construct() {
    } 

    public function search($query = null) { 
        $search = 'http://flickr.com/services/rest/?method=flickr.photos.search&api_key=' . $this->apiKey . '&text=' . urlencode($query) . '&tags=city&sort=relevance&safe_search=1&per_page=1&content_type=1&has_geo=1&format=php_serial'; 
        $result = file_get_contents($search); 
        $result = unserialize($result); 
        return $result; 
    } 
}

I'm not going to include scraper.php, because it's pretty large, but I will if I need to. $location is working just fine.

Thanks!

EDIT: 'scraper.php' is also being used and passing variables to another page to display the weather.

When I just echo '$location' from 'search.php', I get a proper string result at the top, like I mentioned before.

here is the code (search.php):

$Flickr = new Flickr; 
echo $location;

$data = $Flickr->search($location);

The echo command prints nicely at the top. It just won't go into the search function!

9
  • I would try var_dump() on the $location variable that you're getting from the scraper. It's possible that it includes html entities or some other weirdness that's preventing it from working with the Flickr API. Commented Feb 19, 2015 at 2:58
  • Can you var_dump $search after you set it in the search method? Similarly, can you var_dump $result after you run file_get_contents? Commented Feb 19, 2015 at 2:59
  • @KevinM1 I get NULL when I run a var_dump on both. I'm not sure if I'm doing it right, but I'm running the var_dump after the Flickr->search($location) is fired in search.php. Commented Feb 19, 2015 at 4:01
  • @ChrisHerbert var_dump on the $location returns the string just fine, nice and clean. there's no weirdness. the string is being pulled from a JSON coming from the weather underground API. the project started out as a PHP scraper but I later added in the API and never changed the filename. Commented Feb 19, 2015 at 4:08
  • Are you seeing any errors in the log? Commented Feb 19, 2015 at 4:10

2 Answers 2

0

perhaps you have multi file inclusion problem. Please use include_once to include 'scraper.php' as you are including it multiple times.

in flickr.php

include_once('scraper.php');

while searching...

require_once('flickr.php'); 
include_once('scraper.php'); //include or remove this line as it is already included in your flickr.php

$Flickr = new Flickr; 
$data = $Flickr->search($location);
Sign up to request clarification or add additional context in comments.

1 Comment

I gave this a shot, but no luck. Like I said before, I have no problems when I just echo the variable '$location' at the top. I'd like to mention that 'scraper.php' is also in use and passing variables to a separate page which displays the weather. maybe these are causing an issue?
0

Thanks all for your help!

I consolidated my code a bit and included the PHP commands from 'search.php' directly into the file that needed to run the search. I was finally able to pass the variable through. By cutting out the middleman and keeping it all in one file, things began to work just fine. Must of been an inclusion problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.