JSON garbled to Chinese
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 Crawler
Function _grab($curl,$postInfo=‘‘,$cookie=‘‘,$referer=‘‘,$userAgent=‘‘){
$ch = curl_init();
Curl_setopt($ch, CURLOPT_URL, $curl);
/ / No output header
Curl_setopt($ch, CURLOPT_HEADER, 0);
/ / Returns the obtained information as a string, not directly output
Curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
/ / If it is a https link, do not verify the certificate
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);
}
//Add a cookie
If($cookie){
Curl_setopt($ch,CURLOPT_COOKIE,$cookie);
}
/ / Analog to the road
If($referer){
Curl_setopt($ch, CURLOPT_REFERER, $referer);
}
/ / Simulation environment
If($userAgent){
Curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
}
//carried out
$content = curl_exec($ch);
/ / Error handling
If ($content === false) {
Return "Network request error:" . curl_error($ch);
Exit();
}
Return $content;
}
PHP some practical self-made methods