采集天气编码问题 把你的代码测试了,没有问题的,不存在乱码!你看下你的程序文件本身有没有保存成utf8格式! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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的配置呢 传递一个names参数 中文的城市名,我已经采集了问天网的城市id所以,根据城市名称去数据库查询城市的id,接着访问问天网的'http://m.weather.com.cn/data/'.$id.'.html';这个地址可以获取到 json形式的天气情况,刚才测试了没有问题的 既然是你遇到了问题,需要我们帮助你解决那么至少你得给个可供测试的连接吧?你就 '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 正确解析当然,能被 json_decode 正确解析的 json 必然是 utf-8 编码的既然你已经有了<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />那么在这个页面上输出的解析后的结果就不会出现乱码现象但是你的这段代码是有问题的$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);//对一个对象进行编码转换,不报错吗?你说你在公司可以,我表示怀疑 汗,我当时改了一下代码$files = @file_get_contents($url); $files_encoding = mb_detect_encoding($files, array('UTF-8', 'GBK', 'BIG-5')); $files = iconv($files_encoding, 'UTF-8//IGNORE', $files); $data = json_decode($files); var_dump($data);先转码后解的json,因为测试的时候,我来回的修改这里的代码,可能出现偏差,这里的错误与乱码没有关系的,回去看看吧,错的话,我直接在群里找你 呵呵 对取回的 json 是不需要转码的,因为他就是 utf-8 的如果将解码后的数据用于非 utf-8 编码环境,则需要对其中每个元素单独转码 北京急招PHP工程师 求教导航菜单问题。。 php读取excel的问题 wordpress 插件制作,SOS了 有个问题请热心人士回答 我用的是DZ系统 静态不会生成麻烦看下 smarty 怎么转成静态的 如何用PHP把EXCEL表导入Mysql数据库 网站导航的代码,有没有开源的推荐下 谁能帮忙看看这个是用什么加密的,或者有什么方法可以解密。。谢谢大神们了······已经用Dezender解密,不行! 关于PHP中的while跟sleep的mysql查询问题 (高分)为什么这段代码不能实现文件下载
| 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的配置呢
你就 'http://m.weather.com.cn/data/'.$id.'.html' 连 $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 正确解析
当然,能被 json_decode 正确解析的 json 必然是 utf-8 编码的
既然你已经有了
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
那么在这个页面上输出的解析后的结果就不会出现乱码现象但是你的这段代码是有问题的
$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);//对一个对象进行编码转换,不报错吗?
你说你在公司可以,我表示怀疑
汗,我当时改了一下代码
$files = @file_get_contents($url);
$files_encoding = mb_detect_encoding($files, array('UTF-8', 'GBK', 'BIG-5'));
$files = iconv($files_encoding, 'UTF-8//IGNORE', $files);
$data = json_decode($files);
var_dump($data);
先转码后解的json,因为测试的时候,我来回的修改这里的代码,可能出现偏差,这里的错误与乱码没有关系的,回去看看吧,错的话,我直接在群里找你 呵呵
如果将解码后的数据用于非 utf-8 编码环境,则需要对其中每个元素单独转码