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 正确解析
当然,能被 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 没有任何问题,能被 json_decode 正确解析
当然,能被 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 编码环境,则需要对其中每个元素单独转码

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.