Sina weather interface XML processing method, and finally converted into an array to use the code

<?php
//Force page UTF-8 encoding
//header("Content-Type: text/html;charset=UTF-8");
$local = trim($_GET["city"]);
//According to different browsers, return different urlencode encodings
if (strpos($_SERVER["HTTP_USER_AGENT"], "MSIE"))
    //IE series do not need to be converted to GB2312 before encoding, otherwise %00 will be generated in the middle of each encoding junk data, resulting in unusable
    {
    echo "Internet Explorer" . "<br><br>";
    echo urlencode($local) . "<br><br>";
    $ct = urlencode($local);
} else { //Other browsers need to force mb_convert_encoding to be converted to GB2312 before encoding
    echo "Non Internet Explorer" . "<br><br>";
    echo urlencode(mb_convert_encoding($local, "gb2312", "UTF-8 "))." <br><br>";
    $ct = urlencode(mb_convert_encoding($local, "gb2312", "UTF-8"));
}
//The Sina weather interface to be accessed
$url = "http://php.weather.sina.com.cn/xml.php?city=" . $ct .
    "&password=DJOYnieT8234jlsK&day=0";
//Start getting the weather Data
$html = file_get_contents($url);
//echo $html;
$xml = simplexml_load_string($html);
//var_dump($xml); //View the contents of the current object
//echo "<br><br>" ;
//Convert XML content to JSON and then to ARRAY array content
$xml = json_decode(json_encode($xml), true);


//print_r($xml); //Organize XML data
//echo "<br ><br>";


//Data usage method
if (count($xml, 1) <= 1) {
    //I don't know why the beginning is already UTF-8 encoded, but the direct display of Chinese here is garbled, which is actually GB2312, so forced to turn the next
//Find the reason, change the encoding format of the file to UTF-8 and save it
    //echo mb_convert_encoding("The city name is wrong, or there is no weather in the city", "UTF-8", "gb2312") ;
echo "The city name is written incorrectly, or there is no weather for the city";
    echo "<br><br>";
    echo count($xml, 1);
} else {
//Use the contents of the array
    echo trim($xml['Weather']['city'])."<br><br>";
    var_dump($xml);
    echo "<br><br>";
    echo count($xml, 1);
}
?>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325616574&siteId=291194637