在 PHP 中,一个 file_get_contents() 可以接受来自另一个 file_get_contents()



所以我目前正在使用PHP使用Wunderground API。但是,我似乎遇到了一个小障碍。我已经在一个名为 Keys.txt 的文本文件中存储了几个 Wunderground API 密钥。然后,我想检索每个所说的键并将它们存储到一个字符串数组中,然后我可以在其中引用它们以创建一个完整的 URL,该 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 FilesxampphtdocsStructurestest.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 文件中检索到的密钥时,它才会出错。是的,我尝试将所有内容显式键入字符串,但我仍然得到完全相同的结果。关于如何解决这个问题的任何想法?

看起来您正在提取一个 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。

最新更新