网页编码GB2312(“注意这点,比较重要”),代码如下
<?phpprint_r(getKwToData('玻璃移门'));//当这里关键词为"玻璃移门"时,就无法解析json,但生成的url直接通过浏览器访问就有数据,如果关键词为"手机",就可以正常解析jsonfunction 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);//生成的url $data=file_get_contents($url);//这里不管什么关键词都可以获取网页数据 $listItem=json_decode($data);//当这里关键词为"玻璃移门"时,就无法解析json,但生成的url直接通过浏览器访问就有数据,如果关键词为"手机",就可以正常解析json return $listItem->listItem;}function getLocalIp(){//获得用户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;}?>
关键词为"玻璃移门"时,echo $data; 是什么值
关键词为"玻璃移门"时,echo $data; 是什么值
echo 的代码如下,代码太多只能贴出部分,由于是手动截取的listItem节点内的格式可能不正确,或者直接访问(http://110.75.71.20/search_turn_page_iphone.htm?sort=&q=%E7%8E%BB%E7%92%83%E7%A7%BB%E9%97%A8&page=1&showMode=list),注意:我php文件编码是gb2312的,echo 出来的数据在浏览器中必须选utf-8才能正常显示,否则是乱码
{"result":"true","totalPage":"100","catmap":"","ppath":"","category":"移门","auctionTagFlag1":"","auctionTagFlag2":"","auctionTagFlag3":"","listItem":[ {"name":"特价吊门。阳台厨房客厅餐厅隔断推拉门,钛合金材质玻璃隔断移门" ,"img":"http://q.i03.wimg.taobao.com/bao/uploaded/i1/10777020696035707/T12ggQXXBXXXXXXXXX_!!0-item_pic.jpg_90x90.jpg","img2":"http://q.i03.wimg.taobao.com/bao/uploaded/i1/10777020696035707/T12ggQXXBXXXXXXXXX_!!0-item_pic.jpg","iswebp":"","url":"http://a.m.taobao.com/i12812086428.htm?rn=54mFXh1-tGoPo1JS2xg7rHCf0C-90rXM9O-Y7hC&sid=aa444833fda20421","previewUrl":"http://a.m.taobao.com/ajax/pre_view.do?itemId=12812086428&sid=aa444833fda20421","favoriteUrl":"http://fav.m.taobao.com/favorite/to_collection.htm?itemNumId=12812086428&sid=aa444833fda20421", "icon":["0" ], "price":"320.00","originalPrice":"320.00","freight":"0","area":"上海","act":"月售12","itemNumId":"12812086428","nick":"特价移门淋浴房阳台", "wwimUrl":"http://im.m.taobao.com/ww/ad_ww_dialog.htm?item_num_id=12812086428&to_user=zNi829LGw8XB3NSht7%2FR9Myo","category":"50022360","isMobileEcard":"false","auctionType":"b" } , {"name":"卫生间隔断门 阳台玻璃推拉门/拼格门 钢化玻璃移门 新店特惠" ,"img":"http://q.i04.wimg.taobao.com/bao/uploaded/i4/11427033313416399/T1aPluXCdcXXXXXXXX_!!0-item_pic.jpg_90x90.jpg","img2":"http://q.i04.wimg.taobao.com/bao/uploaded/i4/11427033313416399/T1aPluXCdcXXXXXXXX_!!0-item_pic.jpg","iswebp":"","url":"http://a.m.taobao.com/i17422447064.htm?rn=54mFXh1-tGoPo1JS2xg7rHCf0C-90rXM9O-Y7hC&sid=aa444833fda20421","previewUrl":"http://a.m.taobao.com/ajax/pre_view.do?itemId=17422447064&sid=aa444833fda20421","favoriteUrl":"http://fav.m.taobao.com/favorite/to_collection.htm?itemNumId=17422447064&sid=aa444833fda20421", "icon":["0" ], "price":"218.02","originalPrice":"298.00","freight":"10","area":"郑州","act":"月售10","itemNumId":"17422447064","nick":"逸品尚家居", "wwimUrl":"http://im.m.taobao.com/ww/ad_ww_dialog.htm?item_num_id=17422447064&to_user=0t3Gt8nQvNK%2B0w%3D%3D","category":"50022360","isMobileEcard":"false","auctionType":"b" } }
你在json_decode之前将$data做下面的替换工作就可以了
$data = str_replace('\\', '\\\\', $data);
如果不知道为什么这样做,把$data输出的内容放到下面的网址下做个校验就知道了
Just Make JSON Easy!
$s = file_get_contents('http://110.75.71.20/search_turn_page_iphone.htm?sort=&q=%E7%8E%BB%E7%92%83%E7%A7%BB%E9%97%A8&page=1&showMode=list');$s = stripslashes($s);//去掉可能存在的转义$t = json_decode($s, true);//解码成数组array_walk_recursive($t, 'togbk');//因为你需要的是 gbk 编码的,所以要转一下码print_r($t);function togbk(&$item, $key) { $item = iconv('utf-8', 'gbk', $item);}Array
你难道不知道json不支持gbk编码么?虽然不会报错也不会出现乱码,但官方已经声明不支持gbk。所以就出现你说的这种情况了。
我来总结一番:
1.json不支持gbk编码;
2.array_walk_recursive($t, 'togbk');//因为你需要的是 gbk 编码的,所以要转一下码
php中json_decode();解析json数据,如果json里有中文数据,是解析不了的。
stripslashes($abc);//去掉可能存在的转义