PHP Call Picture garbled problem
City Name
$city = ' Changsha ';
Get data in JSON format
$str = file_get_contents ("http://api.map.baidu.com/telematics/v3/weather?location=". $city. " &output=json&ak=kxrzyxh5wusbedo5tkgrqkbz ");
Encode a string in JSON format
$arr = Json_decode ($str, TRUE);
echo "City:". $arr [' Results '][0][' currentcity ']. "Date:". $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 "weather: {$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 ("Image format not supported");
}
echo "
";
echo "< img src =" {$val [' Daypictureurl ']} "/>";
echo "Winds: {$val [' Wind ']}
";
echo "Temp: {$val [' temperature ']}
";
}
?>
The code, as shown above, outputs a string of garbled characters.
Website: http://1.alilo.sinaapp.com/Weather/Weather.php
------to solve the idea----------------------
City Name
$city = "Changsha";
Get data in JSON format
$string = "http://api.map.baidu.com/telematics/v3/weather?location=". $city. " &output=json&ak=kxrzyxh5wusbedo5tkgrqkbz ";
$str = file_get_contents ($string);
Echo $str;
Encode a string in JSON format
$arr = Json_decode ($str, TRUE);
foreach ($arr [' Results '][0][' weather_data ']as $val)
{
echo $val [' Date '].
";
echo "weather: {$val [' Weather ']}
";
$path = "{$val [' Daypictureurl ']}";
Echo ';
echo "
";
echo "
";
echo "< img src =" {$val [' Daypictureurl ']} "/>";
echo "Winds: {$val [' Wind ']}
";
echo "Temp: {$val [' temperature ']}
";
}
?>