PHP not passing variable

偶尔善良 提交于 2019-12-13 01:15:55

问题


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!


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/28597903/php-not-passing-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!