How PHP can parse Json,jsonm_decode sometimes, sometimes not
This post was last edited by cnaaa8 on 2013-06-21 09:55:14
Page encoding GB2312 ("Note this, more important"), the code is as follows
When the keyword here is "glass sliding door", the JSON can not be parsed, but the generated URL directly through the browser access to data, if the keyword is "mobile phone", you can normally parse json,file_get_contents the resulting data seems to be utf-8 format
Print_r (Getkwtodata (' glass sliding door ')); When the keyword here is "glass sliding door", the JSON can not be parsed, but the generated URL directly through the browser access to the data, if the keyword is "phone", it will be able to parse JSON normally
function Getkwtodata ($kw, $page =1, $condition = ")
{
if (empty ($kw)) {
return false;
}
if (Preg_match ("/^ ([". chr (228). " -". Chr (233)." {1} [". chr (128)." -". Chr (191)." {1} [". chr (128)." -". Chr (191)." {1}) {1}/", $kw) = = True | | Preg_match ("/[". chr (228). " -". Chr (233)." {1} [". chr (128)." -". Chr (191)." {1} [". chr (128)." -". Chr (191)." {1}) {1}$/", $kw) = = True | | Preg_match ("/[". chr (228). " -". Chr (233)." {1} [". chr (128)." -". Chr (191)." {1} [". chr (128)." -". Chr (191)." {1}) {2,}/", $kw)! = True)
{
$kw =iconv (' GBK ', ' utf-8 ', $kw);
}
$randIp = ' 110.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 get Web data for any keyword here
$listItem =json_decode ($data);//When the keyword here is "glass sliding door", the JSON cannot be parsed, but the resulting URL can be accessed directly through the browser with data, if the keyword is "phone", it will be able to parse JSON normally
return $listItem->listitem;
}
function Getlocalip () {//Get User IP
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 to:
------Solution--------------------
Stripslashes ($ABC);//Remove possible escapes