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 ');
}
?>
The page has been converted into utf-8 mode, but it's still garbled.
Reply to discussion (solution)
Put your code test, no problem, there is no garbled! You see your program file itself has not been saved into the UTF8 format!
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 |
+----------------------+-----------------+
Look at the configuration of your MySQL.
Put your code test, no problem, there is no garbled! You see your program file itself has not been saved into the UTF8 format!
I tested it on the company's computer and could get to the weather, which is not in my notebook. An estimate is a problem with transcoding
Does your code work correctly?
Please give the correct test parameters
Can the company, at home, not be? Browser, do you see if the browser is set to automatically select the encoding to view it?
Does your code work correctly?
Please give the correct test parameters
Pass a names parameter the city name of the Chinese, I have collected to ask Skynet's city ID so, according to the city name to go to the database to query the city ID, and then visit Skynet's ' http://m.weather.com.cn/data/'. $id. HTML '; This address can get to the JSON form of weather conditions, just tested no problem
Can the company, at home, not be? Browser, do you see if the browser is set to automatically select the encoding to view it?
Look at the browser code is really not the same, I use the Google Browser, but my manual turn is still garbled, go home at night to study under
Well, good luck, hehe.
Now that you have a problem, you need us to help you solve it. So at least you have to give a test connection?
You'll be ' http://m.weather.com.cn/data/'. $id. " HTML ' even $id values are not willing to give?
Now that you have a problem, you need us to help you solve it. So at least you have to give a test connection?
You'll be ' http://m.weather.com.cn/data/'. $id. " HTML ' even $id values are not willing to give?
I'm sorry, for example, my file citys.php?names= Beijing, I will be based on the city name Beijing, check my original collection of city ID
BEIJING-101010100
Shanghai-101020100
Hangzhou-101210101
Then I found that the city ID of Beijing is 101010100, so I will ask
Http://m.weather.com.cn/data/101010100.html this address, check the weather in Beijing city, this is to ask Skynet to the interface address query. I don't know what I'm saying. No, I'm a garbled figure that appears when I get the returned data.
Make the simple things so complicated! How do you get a query address, not our concern?
We just have to observe the retrieved data.
$url = ' http://m.weather.com.cn/data/101010100.html '; $s = file_get_contents ($url);p Rint_r (Json_decode ($s));
StdClass object ([Weatherinfo] = StdClass object ([City] = Beijing [City_en] = bei jing [date_y] = December 18, 2013 [Date] [Week] = Wednesday [Fchh] = 08 [Cityid] = 101010100 [Temp1] = 3℃~-8℃[temp2] = 3℃~-7℃[temp3] =& Gt 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] = clear [Weather3] = clear Turn cloudy [weather4] = Cloudy turn clear [WEATHER5] = clear [WEATHER6] = clear Turn cloudy [IMG1] + 0 [Img2] = [i MG3] [+ 0] [IMG4] = 99 [IMG5] = 0 [Img6] = 1 [Img7] = 1 [Img8] + 0 [img9] = = 0 [Img10] = [IMG11] = 0 [Img12] = 1 [Img_single] + 0 [Img_title1] [img_title2] = clear [Img_title3] [img_title4] = clear [Img_title5] [img_title6] = cloudy [Img_title7] [Img_title8] [Img_title9] = clear [Img_title10] [IMG_TITLE11] = clear [img_ TITLE12] = cloudy [Img_title_single] [wind1] [wind2] = Breeze [Wind3] = Breeze [Wind4] = Breeze [Wind5] = Breeze [Wind6] = Breeze [FX1] =&G T Breeze [FX2] = Breeze [FL1] = less than level 3 [FL2] and less than 3 [fl3] = less than 3 levels [FL4] = less than 3 levels [FL5] = less than level 3 [fl6] and less than Level 3 [index] = cold [index_d] and cold weather, recommended thick down jacket, Fur coat thickened sweater and other winter clothing. In particular, the elderly should pay attention to warm frost. [index48] = cold [index48_d] and cold weather, recommended thick down jacket, fur coats thicker sweater and other winter clothing. In particular, the elderly should pay attention to warm frost. [INDEX_UV] = weak [INDEX48_UV] = weak [INDEX_XC] = suitable [index_tr] = more appropriate [Index_co] = less comfortable [st1] = 2 [ST2] +-5 [ST3] + 3 [ST4] => ; -5 [ST5] + 3 [ST6] +-5 [INDEX_CL] = suitable [Index_ls] = basic suitable [Index_ag] = very difficult to send))
He returned the JSON without any problems and could be correctly parsed by Json_decode
Of course, JSON that can be correctly parsed by Json_decode is bound to be utf-8 encoded.
Now that you've got it,
Then the parsing results on this page will not appear garbled.
But you have a problem with this piece of code.
$data = Json_decode ($files); The JSON string read $files decoded into an object $data $content_encoding = mb_detect_encoding ($data, Array (' UTF-8 ', ' GBK '));//encode an object for identification, Can this be successful? $datas = Iconv ($content _encoding, ' Utf-8//ignore ', $data);//encode and convert an object without error?
You said you could do it in the company, I doubt it.
Make the simple things so complicated! How do you get a query address, not our concern?
We just have to observe the retrieved data.
$url = ' http://m.weather.com.cn/data/101010100.html '; $s = file_get_contents ($url);p Rint_r (Json_decode ($s));
StdClass object ([Weatherinfo] = StdClass object ([City] = Beijing [City_en] = bei jing [date_y] = December 18, 2013 [Date] [Week] = Wednesday [Fchh] = 08 [Cityid] = 101010100 [Temp1] = 3℃~-8℃[temp2] = 3℃~-7℃[temp3] =& Gt 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] = clear [Weather3] = clear Turn cloudy [weather4] = Cloudy turn clear [WEATHER5] = clear [WEATHER6] = clear Turn cloudy [IMG1] + 0 [Img2] = [i MG3] [+ 0] [IMG4] = 99 [IMG5] = 0 [Img6] = 1 [Img7] = 1 [Img8] + 0 [img9] = = 0 [Img10] = [IMG11] = 0 [Img12] = 1 [Img_single] + 0 [Img_title1] [img_title2] = clear [Img_title3] [img_title4] = clear [Img_title5] [img_title6] = cloudy [Img_title7] [Img_title8] [Img_title9] = clear [Img_title10] [IMG_TITLE11] = clear [img_ TITLE12] = cloudy [Img_title_single] [wind1] [wind2] = Breeze [Wind3] = Breeze [Wind4] = Breeze [Wind5] = Breeze [Wind6] = Breeze [FX1] =&G T Breeze [FX2] = Breeze [FL1] = less than level 3 [FL2] and less than 3 [fl3] = less than 3 levels [FL4] = less than 3 levels [FL5] = less than level 3 [fl6] and less than Level 3 [index] = cold [index_d] and cold weather, recommended thick down jacket, Fur coat thickened sweater and other winter clothing. In particular, the elderly should pay attention to warm frost. [index48] = cold [index48_d] and cold weather, recommended thick down jacket, fur coats thicker sweater and other winter clothing. In particular, the elderly should pay attention to warm frost. [INDEX_UV] = weak [INDEX48_UV] = weak [INDEX_XC] = suitable [index_tr] = more appropriate [Index_co] = less comfortable [st1] = 2 [ST2] +-5 [ST3] + 3 [ST4] => ; -5 [ST5] + 3 [ST6] +-5 [INDEX_CL] = suitable [Index_ls] = basic suitable [Index_ag] = very difficult to send))
He returned the JSON without any problems and could be correctly parsed by Json_decode
Of course, JSON that can be correctly parsed by Json_decode is bound to be utf-8 encoded.
Now that you've got it,
Then the parsing results on this page will not appear garbled.
But you have a problem with this piece of code.
$data = Json_decode ($files); The JSON string read $files decoded into an object $data $content_encoding = mb_detect_encoding ($data, Array (' UTF-8 ', ' GBK '));//encode an object for identification, Can this be successful? $datas = Iconv ($content _encoding, ' Utf-8//ignore ', $data);//encode and convert an object without error?
You said you could do it in the company, I doubt it.
Khan, I changed the code a bit.
$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);
First transcoding after the solution of the JSON, because the test, I go back and forth to modify the code here, there may be deviations, here the error is not related to garbled, go back to see it, wrong, I directly in the group to find you hehe
There is no need to transcode the JSON retrieved, because he is utf-8.
If you use decoded data for a non-UTF-8 encoding environment, you need to transcode each of these elements separately