PHP未传递变量 [英] PHP not passing variable

查看:84
本文介绍了PHP未传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个很奇怪的问题,使我完全陷入困境.

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

我正在尝试将PHP变量从一个文件传递到另一个文件,然后将该变量插入Flickr API中的搜索功能.

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.

在上下文中,它拉出包含天气预报位置的变量($ location).由用户输入定义的$ location(位于scraper.php中)进入搜索功能.该函数将请求发送到Flickr API,该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.

进行中:

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

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

请记住,如果我在文件本身中定义搜索字符串,则所有这些都可以很好地工作.只有当我尝试使用scraper.php中的变量时,它根本不搜索任何内容.例如:

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

以上工作正常.更让人困惑的是,如果我在进入搜索功能之前回显"$ location",我会得到正确的结果.变量在那里并且包括在内.出于某种原因,它不会继续执行该功能.

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:

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密钥):

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

我不会包含scraper.php,因为它很大,但是如果需要的话,我会的. $ location工作正常.

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.

谢谢!

"scraper.php"也正在使用中,并将变量传递到另一个页面以显示天气.

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

当我只是从'search.php'回显'$ location'时,就像上面提到的那样,我在顶部得到了正确的字符串结果.

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

这是代码(search.php):

here is the code (search.php):

$Flickr = new Flickr; 
echo $location;

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

echo命令在顶部很好地打印.它只是不会进入搜索功能!

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

推荐答案

感谢您的帮助!

我对代码进行了一些合并,并将来自"search.php"的PHP命令直接包含到运行搜索所需的文件中.我终于能够通过该变量.通过切掉中间人并将其全部保存在一个文件中,一切就可以开始正常工作了.必须是一个包容性问题.

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.

这篇关于PHP未传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆