そのため、現在、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() 呼び出しにレンチを投げるように見えます。キーを 1 つの長い文字列に差し込むだけで機能し、奇妙な "ÿþ" 文字を含む最初の要素を参照しても機能するからです。Keys.txt ファイルから取得したキーを使用しようとするとエラーになります。はい、すべてを明示的に文字列に型キャストしようとしましたが、まったく同じ結果が得られます。この問題を解決する方法について何か考えはありますか?