Collection weather code problems

Source: Internet
Author: User
Collection weather code problems
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', '000000') or die ('MySQL error: '. mysql_error ());
Mysql_select_db ('test ');
Mysql_query ('set names utf8 ');
}
?>
The page has been converted to UTF-8 mode, but it is still garbled


Reply to discussion (solution)

Test your code. No problem, no garbled code! Check whether your program file is saved in 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 |
+ ---------------------- + ----------------- +

What about your mysql configuration?

Test your code. No problem, no garbled code! Check whether your program file is saved in utf8 format!
I tested it on my company's computer. I can get the weather, but I can't do it in my laptop. It is estimated that it is a transcoding problem.

Can your code run correctly?
Provide correct test parameters

Can the company do it? Is the browser the same? have you checked whether the browser has set automatic selection encoding?

Can your code run correctly?
Provide correct test parameters
I have collected the City id of the city where the names parameter is in Chinese. Therefore, query the City id in the database based on the city name, and then access the 'http: // m.weather.com.cn/data/'.$id.'.html'] the address can receive weather conditions in the json format. I just tested that there is no problem.

Can the company do it? Is the browser the same? have you checked whether the browser has set automatic selection encoding?
I saw that the browser code is indeed different. I used Google's browser, but I manually switched it to garbled code. I went home at night and studied it again.

Well, good luck.

Since you have encountered a problem and need our help to solve it, at least you have to provide a connection for testing?
Are you willing to give the $ id value for 'http: // m.weather.com.cn/data/'. mongoid.'.html?

Since you have encountered a problem and need our help to solve it, at least you have to provide a connection for testing?
Are you willing to give the $ id value for 'http: // m.weather.com.cn/data/'. mongoid.'.html?
Sorry, like my file citys. php? Names = Beijing I will query the City id I collected based on the city name Beijing
BEIJING-101010100
SHANGHAI-101020100
HANGZHOU-101210101
Then I found that the city id in Beijing is 101010100, and then I will request
Http://m.weather.com.cn/data/101010100.html this address, to query the weather in Beijing city, this is to query the interface address of the Sky network. I don't know what I'm talking about. I am confused when getting the returned data.

Make simple things so complicated! We are not concerned about how you obtain the query address.
We only need to observe the retrieved 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] => beijing [city_en] => beijing [date_y] => December 18, 2013 [date] => [week] => Wednesday [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] => clear [weatsp2] => clear [weather3] => clear to cloudy [weather4] => cloudy to clear [weather5] => clear [weather6] => clear to cloudy [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] => clear [img_title2] => clear [img_title3] => clear [img_title4] => clear [img_title5] => clear [img_title6] => multi-cloud [img_title7] => multi-cloud [Img_title8] => clear [img_title9] => clear [img_title10] => clear [img_title11] => clear [img_title12] => multi-cloud [img_title_single] => clear [wind1] => Breeze [wind2] => Breeze [wind3] => Breeze [wind4] => Breeze [wind5] => Breeze [wind6] => Breeze [fx1] => Breeze [fx2] => Breeze [fl1] => less than 3 [fl2] => less than 3 [fl3] => less than 3 [fl4] => less than 3 [fl5] => less than 3 levels [fl6] => less than 3 levels [index] => cold [index_d] => cold weather, we recommend that you wear thick down jackets, fur coats, thick sweaters, and other winter clothes. For the elderly and the weak, pay special attention to keeping warm and antifreeze. [Index48] => cold [index48_d] => It is recommended that you wear thick down jackets, fur coats, thick sweaters, and other winter clothes. For the elderly and the weak, pay special attention to keeping warm and antifreeze. [Index_uv] => weak [index48_uv] => weak [index_xc] => suitable for [index_tr] => more suitable for [index_co] => less comfortable [st1] => 2 [st2] =>-5 [st3] => 3 [st4] =>-5 [st5] => 3 [st6] =>-5 [index_cl] => suitable for [index_ls] => basically suitable for [index_ag] => extremely difficult ))
The returned json is correct and can be parsed by json_decode.
Of course, json that can be correctly parsed by json_decode must be UTF-8 encoded.
Now that you have

The parsed results output on this page will not be garbled.

But you have a problem with this code.
$ Data = json_decode ($ files); // The read json string $ files is decoded into an object $ data $ content_encoding = mb_detect_encoding ($ data, array ('utf-8 ', 'gbk'); // code an object. is this successful? $ Datas = iconv ($ content_encoding, 'utf-8 // IGNORE ', $ data); // Encode and convert an object. is no error reported?

You said you could do it at the company. I doubt it.

Make simple things so complicated! We are not concerned about how you obtain the query address.
We only need to observe the retrieved 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] => beijing [city_en] => beijing [date_y] => December 18, 2013 [date] => [week] => Wednesday [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] => clear [weatsp2] => clear [weather3] => clear to cloudy [weather4] => cloudy to clear [weather5] => clear [weather6] => clear to cloudy [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] => clear [img_title2] => clear [img_title3] => clear [img_title4] => clear [img_title5] => clear [img_title6] => multi-cloud [img_title7] => multi-cloud [Img_title8] => clear [img_title9] => clear [img_title10] => clear [img_title11] => clear [img_title12] => multi-cloud [img_title_single] => clear [wind1] => Breeze [wind2] => Breeze [wind3] => Breeze [wind4] => Breeze [wind5] => Breeze [wind6] => Breeze [fx1] => Breeze [fx2] => Breeze [fl1] => less than 3 [fl2] => less than 3 [fl3] => less than 3 [fl4] => less than 3 [fl5] => less than 3 levels [fl6] => less than 3 levels [index] => cold [index_d] => cold weather, we recommend that you wear thick down jackets, fur coats, thick sweaters, and other winter clothes. For the elderly and the weak, pay special attention to keeping warm and antifreeze. [Index48] => cold [index48_d] => It is recommended that you wear thick down jackets, fur coats, thick sweaters, and other winter clothes. For the elderly and the weak, pay special attention to keeping warm and antifreeze. [Index_uv] => weak [index48_uv] => weak [index_xc] => suitable for [index_tr] => more suitable for [index_co] => less comfortable [st1] => 2 [st2] =>-5 [st3] => 3 [st4] =>-5 [st5] => 3 [st6] =>-5 [index_cl] => suitable for [index_ls] => basically suitable for [index_ag] => extremely difficult ))
The returned json is correct and can be parsed by json_decode.
Of course, json that can be correctly parsed by json_decode must be UTF-8 encoded.
Now that you have

The parsed results output on this page will not be garbled.

But you have a problem with this code.
$ Data = json_decode ($ files); // The read json string $ files is decoded into an object $ data $ content_encoding = mb_detect_encoding ($ data, array ('utf-8 ', 'gbk'); // code an object. is this successful? $ Datas = iconv ($ content_encoding, 'utf-8 // IGNORE ', $ data); // Encode and convert an object. is no error reported?

You said you could do it at the company. I doubt it.

Khan, I changed the code.
$ 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 );
After transcoding, the json is parsed. during the test, the code here may be changed back and forth, and there may be deviations. The error here has nothing to do with the garbled code. go back and check it. if it is wrong, I am looking for you directly in the group.

No transcoding is required for the retrieved json, because it is UTF-8
If the decoded data is used in a non-UTF-8 encoding environment, each element needs to be transcoded separately.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.