第一步:在我自己的本地(127.0.0.1) 模拟登陆一个网站 获取到cookie 保存在本地。
第二步:当我正常访问这个网站(http://xxxxxshequ.com) 带着我保存在本地的cookie
朋友做营运 切换很多小号 手动登陆 搞得很麻烦
想让他轻松些 第一步实现起来很简单 主要是第二步有什么办法实现吗?
回复内容: 第一步:在我自己的本地(127.0.0.1) 模拟登陆一个网站 获取到cookie 保存在本地。
第二步:当我正常访问这个网站(http://xxxxxshequ.com) 带着我保存在本地的cookie
朋友做营运 切换很多小号 手动登陆 搞得很麻烦
想让他轻松些 第一步实现起来很简单 主要是第二步有什么办法实现吗?
基于 php/curl 编写的类库, 使用方法见注释.
直接使用, 不用处理cookie(程序自动会处理, cookie信息保存于调用类时传递的参数所指定的文件里).
phpsetreferer('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