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