標籤:pos style create ade url ucs lse 驗證 輸出
//json亂碼轉中文
function decodeUnicode($str){ return preg_replace_callback(‘/\\\\u([0-9a-f]{4})/i‘, create_function( ‘$matches‘, ‘return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");‘ ), $str);}
curl爬蟲
function _grab($curl,$postInfo=‘‘,$cookie=‘‘,$referer=‘‘,$userAgent=‘‘){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $curl); //不輸出頭 curl_setopt($ch, CURLOPT_HEADER, 0); //以字串返回擷取的資訊,不直接輸出 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果是https連結,不驗證認證 if(preg_match(‘/https/i‘, $curl)){ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); } //POST if($postInfo){ curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch,CURLOPT_POSTFIELDS,$postInfo); } //加入cookie if($cookie){ curl_setopt($ch,CURLOPT_COOKIE,$cookie); } //類比來路 if($referer){ curl_setopt($ch, CURLOPT_REFERER, $referer); } //類比環境 if($userAgent){ curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); } //執行 $content = curl_exec($ch); //錯誤處理 if ($content === false) { return "網路請求出錯: " . curl_error($ch); exit(); } return $content;}
php一些實用的自製方法