第一步:在我自己的本地(127.0.0.1) 模拟登陆一个网站 获取到cookie 保存在本地。
第二步:当我正常访问这个网站(http://xxxxxshequ.com) 带着我保存在本地的cookie
朋友做营运 切换很多小号 手动登陆 搞得很麻烦想让他轻松些 第一步实现起来很简单 主要是第二步有什么办法实现吗?
基于 PHP/CURL 编写的类库, 使用方法见注释.
直接使用, 不用处理Cookie(程序自动会处理, Cookie信息保存于调用类时传递的参数所指定的文件里).
<?php
/**
* Author: dds_feng
* Email: dds_feng@qq.com
*
* Example:
* $http = new HttpClient(__DIR__.'/cookie.ck');
* $http->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:
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。