在PHP中,file_get_contents()是否可以接受来自另一个file_get_contents()的变量?

时间:2013-09-11 16:08:28

标签: php string text-files file-get-contents wunderground

所以我目前正在使用PHP使用Wunderground API。但是,我遇到的似乎有一点障碍。我在一个名为Keys.txt的文本文件中存储了几个Wunderground API密钥。然后我想检索每个所说的密钥并将它们存储到一个字符串数组中,然后我可以引用它们来创建一个完整的URL,最终用于进行调用和检索数据。以下是我的代码:

<?php
    // This retrieves my Wunderground keys.
    $keys = file_get_contents('Keys.txt');

    /* This splits the keys properly into an array.
     * While I don't understand why, the first element,
     * equating to the first line, always returns "ÿþ",
     * but otherwise, the array is stored just fine.
     * This is an example of what the array will look
     * like if I have stored 2 keys:
     * 
     * [0] "ÿþ"
     * [1] "0aa00aa0000aa0aa"
     * [2] "1bb1b1b11bbbb1bb"
     * 
     * According to var_dump(), each of these are
     * strings.
     */
    $apiKey = explode(",", $keys);


    /* This concatenates everything together to form
     * the full URL used to make the call to
     * Wunderground and retrieve the current weather
     * alerts of the area. Notice I am using in the
     * first key, e.g. "0aa00aa0000aa0aa", which is
     * contained in apiKey's second element (apiKey[1])
     * which, according to var_dump() is indeed a string
     * variable. That said, this all is stored together
     * into the string variable, URLWithVar, which should
     * then look like this:
     * 
     * "http://api.wunderground.com/api/0aa00aa0000aa0aa/alerts/q/34.933889,-103.760556.json"
     */
    $URLWithVar = 'http://api.wunderground.com/api/' . 
        $apiKey[1] . 
        '/alerts/q/34.933889,-103.760556.json';

    /* Just for giggles, I'll also use those weird
     * characters, "ÿþ", in the first element, too.
     * This variable should now look like this:
     * 
     * http://api.wunderground.com/api/ÿþ/alerts/q/34.933889,-103.760556.json
     */
    $BadURL = 'http://api.wunderground.com/api/' . 
        $apiKey[0] . 
        '/alerts/q/34.933889,-103.760556.json';

    /* For testing purposes, I'm going to also store
     * another string variable, but this time without
     * referencing the string variable that's holding the
     * key.
     */
    $URLWithoutVar = "http://api.wunderground.com/api/0aa00aa0000aa0aa/alerts/q/34.933889,-103.760556.json"

    /* This errors with the following error:
     * Warning: file_get_contents() expects parameter 1 to be a valid path, string given in D:\Program Files\xampp\htdocs\Structures\test.php on line 59
     */
    $alertDataWithVar = file_get_contents($URLWithVar);

    // This works properly.
    $alertDataWithoutVar = file_get_contents($URLWithoutVar);

    /* Interestingly, the URL with the "ÿþ" characters
     * by using apiKey[0] *does* work, though not
     * surprisingly, it only returns Wunderground's
     * own response stating that it was an invalid key.
     * But unlike the first attempt at the call with
     * the real key, this does not truly error, and
     * still calls Wunderground and stores the response
     * it gets from Wunderground.
     */
    $alertDataWithBadURL = file_get_contents($BadURL);
?>

正如你所看到的,它看起来像是存储我的键的方式,因为字符串似乎会给下一个file_get_contents()调用带来麻烦。因为如果我只是在一个长字符串中插入密钥,它就可以工作,如果我引用包含奇怪的“ÿþ”字符的第一个元素,它也可以。当我尝试使用从Keys.txt文件中检索的密钥时,它只会出错。是的,我已经尝试将所有内容明确地转换为字符串,但我仍然得到完全相同的结果。有关如何解决这个问题的任何想法?

1 个答案:

答案 0 :(得分:0)

看来你正在提供一个UTF-8文本文件,但PHP试图将其理解为ASCII。因此,您可能会在字符串中看到您看不到的不可见字节。这就是为什么当你自己键入它时,它可以工作,但是当你从文件中提取它时,它不会。

您可以尝试这样的功能:

function file_get_contents_utf8($fn) {
     $content = file_get_contents($fn);
      return mb_convert_encoding($content, 'UTF-8',
          mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
}

而不是基本file_get_contents,而应该正确处理字符串。

或者,您可以在您喜欢的编辑器中打开Keys.txt文件并将其转换为ASCII。