How does php parse json? jsonm_decode can be used sometimes, sometimes it cannot.

Source: Internet
Author: User
How does php parse json? jsonm_decode can be used sometimes. sometimes it cannot be used. In the end, cnaaa8 edits the webpage code GB2312 from 2013-06-210:55:14 ("pay attention to this, it is important "), the code is as follows. when the keyword here is "glass sliding door", json cannot be parsed, but how does php parse json? jsonm_decode can be used sometimes, sometimes not.
At the end of this post, cnaaa8 edited the webpage code GB2312 at 09:55:14 ("pay attention to this, it is important"). The code is as follows:
When the keyword is "glass sliding door", json cannot be parsed, but the generated url directly accesses data through the browser. if the keyword is "mobile phone", json can be parsed normally, the data obtained by file_get_contents seems to be in UTF-8 format.




Print_r (getKwToData ('glass sliding door'); // when the keyword here is "glass sliding door", json cannot be parsed, but the generated url directly accesses data through the browser, if the keyword is "mobile phone", json can be parsed normally.



Function getKwToData ($ kw, $ page = 1, $ condition = '')
{

If (empty ($ kw )){
Return false;
}
If (preg_match ("/^ ([". chr (1, 228 ). "-". chr (1, 233 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1}) {1}/", $ kw) = true | preg_match ("/([". chr (1, 228 ). "-". chr (1, 233 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1}) {1} $/", $ kw) = true | preg_match ("/([". chr (1, 228 ). "-". chr (1, 233 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1}) {2,}/", $ kw )! = True)
{
$ Kw = iconv ('gbk', 'utf-8', $ kw );
}

$ RandIp = '1970. 75.71.20 ';

$ Url = "http: //". $ randIp. "/search_turn_page_iphone.htm? Sort = ". $ condition." & q = ". urlencode ($ kw)." & page = ". $ page." & showMode = list ";



$ Url = iconv ('gbk', 'utf-8', $ url); // generated url

$ Data = file_get_contents ($ url); // you can obtain webpage data regardless of the keyword.


$ ListItem = json_decode ($ data); // when the keyword here is "glass sliding door", json cannot be parsed, but the generated url directly accesses data through the browser, if the keyword is "mobile phone", json can be parsed normally.

Return $ listItem-> listItem;
}


Function getLocalIp () {// obtain the user ip address
If (isset ($ _ SERVER ['http _ CLIENT_IP ']) {
$ Ip = $ _ SERVER ['http _ CLIENT_IP '];
} Elseif (isset ($ _ SERVER ['http _ X_FORWARDED_FOR ']) {
$ Ip = $ _ SERVER ['http _ X_FORWARDED_FOR '];
} Else {
$ Ip = $ _ SERVER ['remote _ ADDR '];
}
Return $ ip;
}


?>
Share:
------ Solution --------------------
Stripslashes ($ abc); // remove possible escape characters

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.