PHP How to parse json,jsonm_decode sometimes, sometimes not

Source: Internet
Author: User
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
  • 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.