时间:2021-07-01 10:21:17 帮助过:22人阅读
第一步:在我自己的本地(127.0.0.1) 模拟登陆一个网站 获取到cookie 保存在本地。
第二步:当我正常访问这个网站(http://xxxxxshequ.com) 带着我保存在本地的cookie
朋友做营运 切换很多小号 手动登陆 搞得很麻烦
想让他轻松些 第一步实现起来很简单 主要是第二步有什么办法实现吗?
基于 PHP/CURL
编写的类库, 使用方法见注释.
直接使用, 不用处理Cookie(程序自动会处理, Cookie信息保存于调用类时传递的参数所指定的文件里).
php
SetReferer('http://foo.com');//set request referer * echo $http->Get('http://foo.com/');//get * $http->SetProxy('http://127.0.0.1:8888');//set http proxy * echo $http->Post('http://bar.com/xxx', array('a'=>'123', 'b'=>'456'));//post **/ class HttpClient{ private $ch; function __construct($cookieJar){ $this->ch = curl_init(); curl_setopt($this->ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36');//UA curl_setopt($this->ch, CURLOPT_TIMEOUT, 60);//timeout curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, TRUE);//follow redirection curl_setopt($this->ch, CURLOPT_AUTOREFERER, true); curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, FALSE);//ssl curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($this->ch, CURLOPT_COOKIEJAR, $cookieJar);//cookie jar curl_setopt($this->ch, CURLOPT_COOKIEFILE, $cookieJar); } function __destruct(){ curl_close($this->ch); } final public function SetProxy($proxy='http://127.0.0.1:8888'){ //curl_setopt($this->ch, CURLOPT_HTTPPROXYTUNNEL, TRUE); curl_setopt($this->ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);//HTTP proxy //curl_setopt($this->ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);//Socks5 proxy curl_setopt($this->ch, CURLOPT_PROXY, $proxy); } final public function SetReferer($ref=''){ if($ref != ''){ curl_setopt($this->ch, CURLOPT_REFERER, $ref);//Referrer } } final public function SetCookie($ck=''){ if($ck != ''){ curl_setopt($this->ch, CURLOPT_COOKIE, $ck);//Cookie } } final public function Get($url, $header=false, $nobody=false){ curl_setopt($this->ch, CURLOPT_POST, false);//GET curl_setopt($this->ch, CURLOPT_URL, $url); curl_setopt($this->ch, CURLOPT_HEADER, $header);//Response Header curl_setopt($this->ch, CURLOPT_NOBODY, $nobody);//Response Body return curl_exec($this->ch); } final public function Post($url, $data=array(), $header=false, $nobody=false){ curl_setopt($this->ch, CURLOPT_URL, $url); curl_setopt($this->ch, CURLOPT_HEADER, $header);//Response Header curl_setopt($this->ch, CURLOPT_NOBODY, $nobody);//Response Body curl_setopt($this->ch, CURLOPT_POST, true);//POST curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($data));//data return curl_exec($this->ch); } final public function getError(){ return curl_error($this->ch); } } // vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4:
参考:
http://www.piaoyi.org/php/php-curl-cookies.html
http://stackoverflow.com/questions/12885538/php-curl-and-cookies