set_time_limit(0);
connect_sql();
$names = trim($_GET['names']);
$id = get_id($names);
if($id){
$url = 'http://m.weather.com.cn/data/'.$id.'.html';
$host = 'http://www.weather.com.cn/';
$files = @file_get_contents($url);
$data = json_decode($files);
$content_encoding = mb_detect_encoding($data, array('UTF-8', 'GBK'));
$datas = iconv($content_encoding, 'UTF-8//IGNORE', $data);
var_dump($data);
}
function get_id($names){
$sql = "SELECT city_bh FROM city_id WHERE city_name = '{$names}'";
$rel = mysql_query($sql);
$row = mysql_fetch_array($rel);
$id = $row['city_bh'];
return $id;
}
function connect_sql(){
$con = mysql_connect('localhost','root','123456') or die('mysql error :'.mysql_error());
mysql_select_db('test');
mysql_query('set names utf8');
}
?>
页面已经转换成utf-8模式了,可是一直还是乱码
把你的代码测试了,没有问题的,不存在乱码!你看下你的程序文件本身有没有保存成utf8格式!
mysql> show variables like 'char%';
+--------------------------+---------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/|
+--------------------------+---------------------------------+
mysql> show variables like 'collation%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+
看看你的mysql的配置呢
把你的代码测试了,没有问题的,不存在乱码!你看下你的程序文件本身有没有保存成utf8格式!
我在公司的电脑上测试了,可以获取到天气,在家里的笔记本上就是不行。估计是转码的问题
你的代码能正确运行?
请给出正确的测试参数
公司可以,家里不可以?浏览器一样吗,你看看浏览器有没有设置自动选择编码查看呢
你的代码能正确运行?
请给出正确的测试参数
传递一个names参数 中文的城市名,我已经采集了问天网的城市id所以,根据城市名称去数据库查询城市的id,接着访问问天网的'http://m.weather.com.cn/data/'.$id.'.html';这个地址可以获取到 json形式的天气情况,刚才测试了没有问题的
公司可以,家里不可以?浏览器一样吗,你看看浏览器有没有设置自动选择编码查看呢
看了浏览器编码确实不一样,我用的谷歌的浏览器,但是我手动的转了仍然是乱码,晚上回家再研究下
恩,好运,呵呵
既然是你遇到了问题,需要我们帮助你解决那么至少你得给个可供测试的连接吧?
你就 'http://m.weather.com.cn/data/'. $id.'.html' 连 $id 的值都不舍得给?
既然是你遇到了问题,需要我们帮助你解决那么至少你得给个可供测试的连接吧?
你就 'http://m.weather.com.cn/data/'. $id.'.html' 连 $id 的值都不舍得给?
额 不好意思,比如我的文件citys.php?names=北京我会根据 城市名称北京,查询我原来采集的城市id
北京-101010100
上海-101020100
杭州-101210101
那么我查到了北京的城市id为101010100,这样就会去请求
http://m.weather.com.cn/data/101010100.html 这个地址,查询到了北京城市的天气,这个是问天网给的接口地址查询的。不知道我说的清楚不,我是在获取返回的数据的时候出现的乱码
把简单的事情弄得那么复杂!你怎么取得查询地址,不是我们关心的
我们只需要观察到取回的数据就可以了
$url = 'http://m.weather.com.cn/data/101010100.html';$s = file_get_contents($url);print_r(json_decode($s));
stdClass Object( [weatherinfo] => stdClass Object ( [city] => 北京 [city_en] => beijing [date_y] => 2013年12月18日 [date] => [week] => 星期三 [fchh] => 08 [cityid] => 101010100 [temp1] => 3℃~-8℃ [temp2] => 3℃~-7℃ [temp3] => 4℃~-6℃ [temp4] => 4℃~-6℃ [temp5] => 5℃~-6℃ [temp6] => 6℃~-2℃ [tempF1] => 37.4?~17.6? [tempF2] => 37.4?~19.4? [tempF3] => 39.2?~21.2? [tempF4] => 39.2?~21.2? [tempF5] => 41?~21.2? [tempF6] => 42.8?~28.4? [weather1] => 晴 [weather2] => 晴 [weather3] => 晴转多云 [weather4] => 多云转晴 [weather5] => 晴 [weather6] => 晴转多云 [img1] => 0 [img2] => 99 [img3] => 0 [img4] => 99 [img5] => 0 [img6] => 1 [img7] => 1 [img8] => 0 [img9] => 0 [img10] => 99 [img11] => 0 [img12] => 1 [img_single] => 0 [img_title1] => 晴 [img_title2] => 晴 [img_title3] => 晴 [img_title4] => 晴 [img_title5] => 晴 [img_title6] => 多云 [img_title7] => 多云 [img_title8] => 晴 [img_title9] => 晴 [img_title10] => 晴 [img_title11] => 晴 [img_title12] => 多云 [img_title_single] => 晴 [wind1] => 微风 [wind2] => 微风 [wind3] => 微风 [wind4] => 微风 [wind5] => 微风 [wind6] => 微风 [fx1] => 微风 [fx2] => 微风 [fl1] => 小于3级 [fl2] => 小于3级 [fl3] => 小于3级 [fl4] => 小于3级 [fl5] => 小于3级 [fl6] => 小于3级 [index] => 寒冷 [index_d] => 天气寒冷,建议着厚羽绒服、毛皮大衣加厚毛衣等隆冬服装。年老体弱者尤其要注意保暖防冻。 [index48] => 寒冷 [index48_d] => 天气寒冷,建议着厚羽绒服、毛皮大衣加厚毛衣等隆冬服装。年老体弱者尤其要注意保暖防冻。 [index_uv] => 弱 [index48_uv] => 弱 [index_xc] => 适宜 [index_tr] => 较适宜 [index_co] => 较不舒适 [st1] => 2 [st2] => -5 [st3] => 3 [st4] => -5 [st5] => 3 [st6] => -5 [index_cl] => 适宜 [index_ls] => 基本适宜 [index_ag] => 极不易发 ))他返回的 json 没有任何问题,能被 json_decode 正确解析
$data = json_decode($files); //读取到的 json 串 $files 解码成对象 $data$content_encoding = mb_detect_encoding($data, array('UTF-8', 'GBK'));//对一个对象进行编码识别,这能成功吗?$datas = iconv($content_encoding, 'UTF-8//IGNORE', $data);//对一个对象进行编码转换,不报错吗?
把简单的事情弄得那么复杂!你怎么取得查询地址,不是我们关心的
我们只需要观察到取回的数据就可以了
$url = 'http://m.weather.com.cn/data/101010100.html';$s = file_get_contents($url);print_r(json_decode($s));
stdClass Object( [weatherinfo] => stdClass Object ( [city] => 北京 [city_en] => beijing [date_y] => 2013年12月18日 [date] => [week] => 星期三 [fchh] => 08 [cityid] => 101010100 [temp1] => 3℃~-8℃ [temp2] => 3℃~-7℃ [temp3] => 4℃~-6℃ [temp4] => 4℃~-6℃ [temp5] => 5℃~-6℃ [temp6] => 6℃~-2℃ [tempF1] => 37.4?~17.6? [tempF2] => 37.4?~19.4? [tempF3] => 39.2?~21.2? [tempF4] => 39.2?~21.2? [tempF5] => 41?~21.2? [tempF6] => 42.8?~28.4? [weather1] => 晴 [weather2] => 晴 [weather3] => 晴转多云 [weather4] => 多云转晴 [weather5] => 晴 [weather6] => 晴转多云 [img1] => 0 [img2] => 99 [img3] => 0 [img4] => 99 [img5] => 0 [img6] => 1 [img7] => 1 [img8] => 0 [img9] => 0 [img10] => 99 [img11] => 0 [img12] => 1 [img_single] => 0 [img_title1] => 晴 [img_title2] => 晴 [img_title3] => 晴 [img_title4] => 晴 [img_title5] => 晴 [img_title6] => 多云 [img_title7] => 多云 [img_title8] => 晴 [img_title9] => 晴 [img_title10] => 晴 [img_title11] => 晴 [img_title12] => 多云 [img_title_single] => 晴 [wind1] => 微风 [wind2] => 微风 [wind3] => 微风 [wind4] => 微风 [wind5] => 微风 [wind6] => 微风 [fx1] => 微风 [fx2] => 微风 [fl1] => 小于3级 [fl2] => 小于3级 [fl3] => 小于3级 [fl4] => 小于3级 [fl5] => 小于3级 [fl6] => 小于3级 [index] => 寒冷 [index_d] => 天气寒冷,建议着厚羽绒服、毛皮大衣加厚毛衣等隆冬服装。年老体弱者尤其要注意保暖防冻。 [index48] => 寒冷 [index48_d] => 天气寒冷,建议着厚羽绒服、毛皮大衣加厚毛衣等隆冬服装。年老体弱者尤其要注意保暖防冻。 [index_uv] => 弱 [index48_uv] => 弱 [index_xc] => 适宜 [index_tr] => 较适宜 [index_co] => 较不舒适 [st1] => 2 [st2] => -5 [st3] => 3 [st4] => -5 [st5] => 3 [st6] => -5 [index_cl] => 适宜 [index_ls] => 基本适宜 [index_ag] => 极不易发 ))他返回的 json 没有任何问题,能被 json_decode 正确解析
$data = json_decode($files); //读取到的 json 串 $files 解码成对象 $data$content_encoding = mb_detect_encoding($data, array('UTF-8', 'GBK'));//对一个对象进行编码识别,这能成功吗?$datas = iconv($content_encoding, 'UTF-8//IGNORE', $data);//对一个对象进行编码转换,不报错吗?
对取回的 json 是不需要转码的,因为他就是 utf-8 的
如果将解码后的数据用于非 utf-8 编码环境,则需要对其中每个元素单独转码