Question

So I am currently working with the Wunderground API using PHP. However, there seems to be a little snag I've encountered. I have stored a couple Wunderground API keys in a text file named Keys.txt. I then want to retrieve each said key and store them into an array of strings, where I can then reference them to make a complete URL that will eventually be used to make the call and retrieve the data. Below is my code:

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

As you can see, it seems like the way it's storing my keys as strings seem to throw a wrench into the next file_get_contents() call. Because if I just plug in the key in one long string, it works, and if I reference that first element which contains the weird "ÿþ" characters, it works as well. It only errors when I try to use the keys that were retrieved from the Keys.txt file. And yes, I have tried typecasting everything explicitly into strings and I still get the exact same results. Any thoughts on how to solve this problem?

Was it helpful?

Solution

It appears you are pulling in a UTF-8 text file, but PHP is trying to understand it as ASCII. So you are likely getting invisible bytes in your string that you aren't seeing. That is why when you type it in yourself, it works, but when you pull it from the file, it doesn't.

You can attempt a function like this:

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

Instead of the base file_get_contents and that should handle the string properly.

Or, you can open your Keys.txt file in your favorite editor and convert it to ASCII.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top