phpでjsonが読み込めない
by
phpでjsonが読み込めないさん
(e32e187b)
投稿数:1回
(ベストアンサー:1回)
(ベストアンサー:1回)
2022/05/12 17:35
xfreeの「php・mysqlサーバー」で、気象庁のjsonファイルから天気のデータを取得しようとしたところ、Warning: file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0というのが表示されました。
googleで検索したのですが、「allow_url_fopen」をonにする的なことが書いてありました。
しかし、xfreeではどうもそれが出来ないようで、どうすればいいか困っています。
どうすればよいでしょうか?
以下、コード
<?php
$url = "https://www.jma.go.jp/bosai/forecast/data/forecast/130000.json";
$weather_json = file_get_contents($url);
$weather_array = json_decode($weather_json, true);
$date = $weather_array["0"]["timeSeries"]["0"]["timeDefines"]["0"];
$jma_weather = $weather_array["0"]["timeSeries"]["0"]["areas"]["0"]["weathers"]["0"];
$jma_rainfall = $weather_array["Feature"]["0"]["Property"]["WeatherList"]["Weather"]["0"]["Rainfall"];
echo "日時:" . $date . "\n";
echo "今日の天気:" . $jma_weather. "\n";
echo "雨量:" . $jma_rainfall. "\n";
?>
googleで検索したのですが、「allow_url_fopen」をonにする的なことが書いてありました。
しかし、xfreeではどうもそれが出来ないようで、どうすればいいか困っています。
どうすればよいでしょうか?
以下、コード
<?php
$url = "https://www.jma.go.jp/bosai/forecast/data/forecast/130000.json";
$weather_json = file_get_contents($url);
$weather_array = json_decode($weather_json, true);
$date = $weather_array["0"]["timeSeries"]["0"]["timeDefines"]["0"];
$jma_weather = $weather_array["0"]["timeSeries"]["0"]["areas"]["0"]["weathers"]["0"];
$jma_rainfall = $weather_array["Feature"]["0"]["Property"]["WeatherList"]["Weather"]["0"]["Rainfall"];
echo "日時:" . $date . "\n";
echo "今日の天気:" . $jma_weather. "\n";
echo "雨量:" . $jma_rainfall. "\n";
?>
by
林檎さん
(e5c2c793)
投稿数:15回
(ベストアンサー:4回)
(ベストアンサー:4回)
2022/05/12 22:19
cURL は使えるようなので以下のサンプルのように 外部url からファイルを読み込めると思います。
<?php
// file_get_contnts 代替
function my_file_get_contents($url)
{
$cp = curl_init();
curl_setopt($cp, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cp, CURLOPT_URL, $url);
curl_setopt($cp, CURLOPT_TIMEOUT, 30);
curl_setopt($cp, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$data = curl_exec($cp);
curl_close($cp);
return $data;
}
$url="https://www.jma.go.jp/bosai/forecast/data/forecast/130000.json";
$weather_json = my_file_get_contents($url);
$weather_array = json_decode($weather_json, true);
$date = $weather_array["0"]["timeSeries"]["0"]["timeDefines"]["0"];
$jma_weather = $weather_array["0"]["timeSeries"]["0"]["areas"]["0"]["weathers"]["0"];
$jma_rainfall = $weather_array["Feature"]["0"]["Property"]["WeatherList"]["Weather"]["0"]["Rainfall"];
echo "日時:" . $date . "\n";
echo "今日の天気:" . $jma_weather. "\n";
echo "雨量:" . $jma_rainfall. "\n";
?>
<?php
// file_get_contnts 代替
function my_file_get_contents($url)
{
$cp = curl_init();
curl_setopt($cp, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cp, CURLOPT_URL, $url);
curl_setopt($cp, CURLOPT_TIMEOUT, 30);
curl_setopt($cp, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$data = curl_exec($cp);
curl_close($cp);
return $data;
}
$url="https://www.jma.go.jp/bosai/forecast/data/forecast/130000.json";
$weather_json = my_file_get_contents($url);
$weather_array = json_decode($weather_json, true);
$date = $weather_array["0"]["timeSeries"]["0"]["timeDefines"]["0"];
$jma_weather = $weather_array["0"]["timeSeries"]["0"]["areas"]["0"]["weathers"]["0"];
$jma_rainfall = $weather_array["Feature"]["0"]["Property"]["WeatherList"]["Weather"]["0"]["Rainfall"];
echo "日時:" . $date . "\n";
echo "今日の天気:" . $jma_weather. "\n";
echo "雨量:" . $jma_rainfall. "\n";
?>