PHP Call picture garbled related issues

Source: Internet
Author: User
Tags imagejpeg
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 ']}

";

}


?>
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.