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!

jmariano
  • 35
  • 7
  • 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. – Chris Herbert Feb 19 '15 at 02: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? – Major Productions Feb 19 '15 at 02: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. – jmariano Feb 19 '15 at 04: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. – jmariano Feb 19 '15 at 04:08
  • Are you seeing any errors in the log? – Chris Herbert Feb 19 '15 at 04:10
  • @ChrisHerbert no errors! clean as a whistle! – jmariano Feb 19 '15 at 04:20
  • Try echoing `$query` within the Flickr class's search method, if the variable is getting passed correctly. Also, remove `include 'scraper.php';` from Flickr.php as it is already included earlier or use `include_once` and see. – web-nomad Feb 19 '15 at 04:27
  • @web-nomad I removed 'scraper.php' from flickr.php. I can't get `$query` to echo anything. where should I be placing the echo command? – jmariano Feb 19 '15 at 04:38
  • Like this `public function search($query = null) { echo $query;die;`. If the search method is getting the variable, then probably an issue with the api call. – web-nomad Feb 19 '15 at 04:50

2 Answers2

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);
  • 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? – jmariano Feb 19 '15 at 04:05
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.

jmariano
  • 35
  • 7