代码如上所示,输出来的是一串乱码。网站:http://1">
当前位置:Gxlcms > PHP教程 > php调用图片乱码有关问题

php调用图片乱码有关问题

时间:2021-07-01 10:21:17 帮助过:17人阅读

php调用图片乱码问题


//城市名
$city = '长沙';
//获取json格式的数据
$str = file_get_contents("http://api.map.baidu.com/telematics/v3/weather?location=".$city."&output=json&ak=KXrZyXh5wUSBEdo5TkgRQkBz");

//对json格式的字符串进行编码
$arr = json_decode($str,TRUE);

echo "城市:".$arr['results'][0]['currentCity']." 日期:".$arr['date']."

";

$wea = "http://1.alilo.sinaapp.com/Weather/0.jpeg";
$weai = imagecreatefromgif($wea);
header("Content-type:image/jpeg");
imagejpeg($weai);

foreach($arr['results'][0]['weather_data']as $val)
{
echo $val['date']."
";
echo "天气:{$val['weather']}
";

$path = "{$val['dayPictureUrl']}";
echo "$path";
$imgInfo = getimagesize($path);
$imgType = $imgInfo[2];
switch ($imgType) {
case 1:
$img = imagecreatefromgif($path);
header("Content-type:image/gif");
imagegif($img);
break;
case 2 :
$img = imagecreatefromjpeg($path);
header("Content-type:image/jpeg");
imagejpeg($img);
break;
case 3 :
$img = imagecreatefrompng($path);
header("Content-type:image/png");
imagepng($img);
break;
default:
exit("图片格式不支持");
}
echo "
";
// echo "< img src = "{$val['dayPictureUrl']}" />";
echo "风向:{$val['wind']}
";
echo "温度:{$val['temperature']}

";
}
?>

代码如上所示,输出来的是一串乱码。
网站:http://1.alilo.sinaapp.com/Weather/Weather.php
------解决思路----------------------


//城市名
$city = "长沙";
//获取json格式的数据
$string ="http://api.map.baidu.com/telematics/v3/weather?location=".$city."&output=json&ak=KXrZyXh5wUSBEdo5TkgRQkBz";
$str = file_get_contents($string);

//echo $str;
//对json格式的字符串进行编码
$arr = json_decode($str,TRUE);


foreach($arr['results'][0]['weather_data']as $val)
{
echo $val['date']."
";
echo "天气:{$val['weather']}
";

$path = "{$val['dayPictureUrl']}";
echo '';
echo "
";
echo "
";
// echo "< img src = "{$val['dayPictureUrl']}" />";
echo "风向:{$val['wind']}
";
echo "温度:{$val['temperature']}

";

}


?>

人气教程排行