Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中,一个文件能否接受另一个文件的变量?_Php_String_Text Files_File Get Contents_Wunderground - Fatal编程技术网

在PHP中,一个文件能否接受另一个文件的变量?

在PHP中,一个文件能否接受另一个文件的变量?,php,string,text-files,file-get-contents,wunderground,Php,String,Text Files,File Get Contents,Wunderground,因此,我目前正在使用PHP处理WundergroundAPI。然而,我似乎遇到了一个小小的障碍。我在一个名为keys.txt的文本文件中存储了几个Wunderground API密钥。然后我想检索每个键并将它们存储到一个字符串数组中,在这里我可以引用它们来生成一个完整的URL,最终将用于进行调用和检索数据。下面是我的代码: <?php // This retrieves my Wunderground keys. $keys = file_get_contents('Key

因此,我目前正在使用PHP处理WundergroundAPI。然而,我似乎遇到了一个小小的障碍。我在一个名为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);
?>


正如您所看到的,它似乎将我的键存储为字符串,这似乎对下一个文件\u get\u 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));
}
而不是基本的
文件\u get\u contents
,这样可以正确处理字符串

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

UTF-8 BOM=奇怪的“ÿþ”字符,我想,尝试使用file而不是file_get_内容来读取keys.txt