2017-11-05 11 views
-1
<?php 
echo "<h1>What's The Weather Like?</h1>"; 
define('API_KEY','********'); 

$apikey = API_KEY; 

$user_ip = '183.91.3.13'; // Remove comment to use this to test when you put 
    this file in localhost 
//$user_ip = $_SERVER['REMOTE_ADDR']; // Comment out this line if you test 
in your localhost 
    $details = 
json_decode(file_get_contents("http://ipinfo.io/{$user_ip}/json")); 
$city_name = $details->city; 
$loc_details = 
json_decode(file_get_contents("http://dataservice.accuweather.com/locations/v1/ cities/search?q={$city_name}&apikey={$apikey}")); 
$loc_key = $loc_details[0]->Key; 
$weather_details = 


json_decode(file_get_contents("http://dataservice.accuweather.com/forecasts/v1/daily/1day/{$loc_key}?apikey={$apikey}")); 
// print_r($weather_details); 
// $result = json_decode($data, true); 
echo $weather_details[0]->EffectiveDate; 
?> 

私はそれがこの問題 致命的なエラーを示しウェブ実行しよう:型のオブジェクトを使用することはできませんがCでの配列としてはstdClass:あなたは(json_decodeを使用して)PHPでJSONをデコードするとき、私は致命的なエラー:Cでの配列としてのタイプはstdClassのオブジェクトを使用することはできません: xamppの htdocsに AIW index.phpを

+0

取得したJSON出力の例を提供します。また、一般的にあなたのAPIキーを公開することはお勧めできません。空の文字列や星の行に置き換えてください。 – Flying

答えて

0

新しい学習者ですので\ xamppの\ htdocsに\ AIW \ライン16 にはindex.php私はあなたが嫌い​​いけない願っています。データをオブジェクトとして返します。データを配列として使用するには、2番目のparamをtrueにする必要があります。だから、以下のようにコードを変更:

<?php 
echo "<h1>What's The Weather Like?</h1>"; 
define('API_KEY','**********'); 

$apikey = API_KEY; 

$user_ip = '183.91.3.13'; // Remove comment to use this to test when you put 
    this file in localhost 
//$user_ip = $_SERVER['REMOTE_ADDR']; // Comment out this line if you test 
in your localhost 
    $details = 
json_decode(file_get_contents("http://ipinfo.io/{$user_ip}/json")); 
$city_name = $details->city; 
$loc_details = 
json_decode(file_get_contents("http://dataservice.accuweather.com/locations/v1/ cities/search?q={$city_name}&apikey={$apikey}"),true); //change here 
$loc_key = $loc_details[0]['Key']; 
$weather_details = 


json_decode(file_get_contents("http://dataservice.accuweather.com/forecasts/v1/daily/1day/{$loc_key}?apikey={$apikey}"),true); //change here 
// print_r($weather_details); 
// $result = json_decode($data, true); 
echo $weather_details[0]['EffectiveDate']; 
?> 
+0

警告:file_get_contents(http:// ******):ストリームのオープンに失敗しました:HTTPリクエストに失敗しました! HTTP/1.1 503 C:\ xampp \ htdocs \ aiw \ index.php(13行目) – Han

+0

編集時にこのエラーが発生する – Han

0

このエラー:

Cannot use object of type stdClass as array 

は、それが配列ではない手段は、それがこのオブジェクト

変更します:

echo $weather_details[0]->EffectiveDate; 

この

echo $weather_details->Headline->EffectiveDate 
関連する問題