curl simulation

header ( 'Content-type: text / HTML; charset = UTF-. 8');
function curlPost (URL $, $ Data, $ Method) {
$ curl_init CH = (); // initialize. 1.
curl_setopt ($ CH, CURLOPT_URL to , $ url); // 2 requested address.
curl_setopt ($ CH, CURLOPT_CUSTOMREQUEST, $ Method); // request. 3 embodiment
. @ 4 as parameters
curl_setopt ($ CH, CURLOPT_SSL_VERIFYPEER, FALSE); // HTTPS
curl_setopt ($ CH, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt ($ CH, CURLOPT_USERAGENT, 'Mozilla / 5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); // simulate browser
curl_setopt ($ CH, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ CH , CURLOPT_AUTOREFERER,. 1);
curl_setopt ($ CH, CURLOPT_HTTPHEADER, Array ( 'the Accept-Encoding: the gzip, the deflate')); // the gzip decompression of the contents
curl_setopt ($ ch, CURLOPT_ENCODING, ' gzip, deflate');

if($method=="POST"){//5.post方式的时候添加数据  
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
}  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
$tmpInfo = curl_exec($ch);//6.执行  

if (curl_errno($ch)) {//7.如果出错  
    return curl_error($ch);  
}  
curl_close($ch);//8.关闭  
return $tmpInfo;  

}
$data=array(‘name’ => ‘1234’);
$url=”http://www.sohu.com/“;

$method=”GET”;
$file=curlPost($url,$data,$method);
$file=mb_convert_encoding($file,’UTF-8’,’GBK’);
echo $file;

When the cookie authentication login
[php] view plain copy viewed on a piece of code derived CODE to my piece of code
<PHP?
$ Cookie_file = tempnam ( './ the TEMP', 'cookie');
function weixinPost ($ url, $ Data, Method $, $ = setcooke to false, $ = cookie_file to false) {
$ curl_init CH = (); // initialize. 1.
curl_setopt ($ ch, CURLOPT_URL to, $ URL);. 2 // request address
curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, $ method);. // 3 requests embodiment
. @ 4 as parameters
curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE large column  curl simulation );
curl_setopt ($ CH, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt ($ CH, CURLOPT_USERAGENT, ' mozilla / 5.0 (compatible; MSIE 5.01; the Windows NT 5.0) ');
curl_setopt ($ CH, CURLOPT_FOLLOWLOCATION,. 1);
curl_setopt ($ CH, CURLOPT_AUTOREFERER,. 1);

    if($method=="POST"){//5.post方式的时候添加数据     
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
    }  
    if($setcooke==true){  
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);  
    }else{  
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);  
    }  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    $tmpInfo = curl_exec($ch);//6.执行  

    if (curl_errno($ch)) {//7.如果出错  
        return curl_error($ch);  
    }  
    curl_close($ch);//8.关闭  
    return $tmpInfo;  
}  
$data=array('username' => '***','password'=>'***');  
$url="http://www.xinxinj.com/login.php";  
$method="POST";  
$file=weixinPost($url,$data,$method,true,$cookie_file);  
echo $file;  

$url="http://www.xinxinj.com/admin.php";  
$method="GET";  
$file=weixinPost($url,$data,$method,false,$cookie_file);  
echo $file;  

?>

If this does not solve, then the following scheme
[php] view plain copy See piece of code is derived to my piece of code in the CODE
$ CH = curl_init ();
$ URL = ' * ';
$ header = Array (
'Cookie: ** '
);
// add to apikey header
curl_setopt ($ CH, CURLOPT_HTTPHEADER, $ header);
// curl_setopt ($ CH, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ CH, CURLOPT_USERAGENT,' Mozilla / 5.0 (Macintosh; Intel Mac 10.12 X-the OS; RV: 50.0) the Gecko / Firefox 20,100,101 / 50.0 ');
curl_setopt ($ CH, CURLOPT_FOLLOWLOCATION,. 1);
curl_setopt ($ CH, CURLOPT_AUTOREFERER,. 1);
// perform HTTP request
curl_setopt ($ ch, CURLOPT_URL, $ URL);
$ the curl_exec RES = ($ CH);
var_dump ($ RES);

Cookie contents of the array above $ header is now in the browser to log in, and then turn on the console, paste the information to come in the cookie


Guess you like

Origin www.cnblogs.com/lijianming180/p/12366299.html