http://www.ayandy.com/Service.asmx?op=getWeatherbyCityName
以上这个页面提供天气预报的web Services,我用php的file_get_contents去读取的时候,却是乱码(返回的xml中城市那一栏)GET的URL:http://www.ayandy.com/Service.asmx/getWeatherbyCityName?theCityName=上海&theDayFlag=1
          http://www.ayandy.com/Service.asmx/getWeatherbyCityName?theCityName=%C9%CF%BA%A3&theDayFlag=1都没有识别出来城市,奇怪了...有办法用GET方法返回正确的xml吗?

解决方案 »

  1.   

    你对汉字进行编码的格式不对,具体我也不太懂,你用这个链接就行了:
    http://www.ayandy.com/Service.asmx/getWeatherbyCityName?theCityName=%E4%B8%8A%E6%B5%B7&theDayFlag=1
      

  2.   

    要想在GET里带汉字,最好先进行编码,PHP里相应的函数我不知是哪个javascript的我倒是知道:<script type="text/javascript">
    var str = encodeURI('上海');
    document.write(str);
    </script>
    http://www.ayandy.com/Service.asmx/getWeatherbyCityName?theCityName=%E4%B8%8A%E6%B5%B7&theDayFlag=1
    把红色部分替换为javascript输出的字符串就行了
      

  3.   

    抱歉,红色部分的范围弄错了,应该是:
    http://www.ayandy.com/Service.asmx/getWeatherbyCityName?theCityName=%E4%B8%8A%E6%B5%B7&theDayFlag=1
      

  4.   

    我又去试了一下,你原来是用PHP的urlencode()函数得到“theCityName=%C9%CF%BA%A3”这个的吧,用这个函数是没错的,只是你的那个PHP文件,就是xxx.php的编码格式错了。
    因为那个XML是以UTF-8模式编码的,你要想通过urlencode()得到正确的编码,PHP文件的编码也得是UTF-8。实施方法是:
    1.用记事本打开你那个xxx.php文件。
    2.点另存为,在下面那个“编码”下拉列表里,选择“UTF-8”。
    3.保持。
    然后你再调用urlencode('上海')就不会得到“%C9%CF%BA%A3”,而是正确的“%E4%B8%8A%E6%B5%B7 ”了
      

  5.   


    $url = "http://www.ayandy.com/Service.asmx/getWeatherbyCityName";
    $ch = curl_init() ;
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POSTFIELDS,"?action=''&theCityName=上海&theDayFlag=1");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $result = curl_exec($ch);
    echo htmlspecialchars($result);
    curl_close($ch);
    结果<?xml version="1.0" encoding="utf-8"?> <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">  <string xsi:nil="true" />  <string>上海</string>  <string>阴转多云</string>  <string>24 ~ 14 ℃</string>  <string>微风转东风3-4级</string>  <string>今天</string>  <string>http://www.ayandy.com/images/阴转多云.gif</string>  <string xsi:nil="true" /> </ArrayOfString>