采集天气编码问题
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_decode 正确解析的 json 必然是 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);//对一个对象进行编码转换,不报错吗?
你说你在公司可以,我表示怀疑
把简单的事情弄得那么复杂!你怎么取得查询地址,不是我们关心的
我们只需要观察到取回的数据就可以了
$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_decode 正确解析的 json 必然是 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 编码环境,则需要对其中每个元素单独转码

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
