class proxy_http {
//use agent
private $agent = '';//代理
//get or post
private $method = "get";
//get data from where
private $url;
//parameters post
private $postparam = array();
//referer
private $referer;
//超时
private $timeout = 30;
//cookie
private $cookie;
// 是否直接输出数据,避免内存溢出
private $isdirectoutput = false;
/**
*设置agent
*@param
*/
public function setagent($agent) {
$this->agent = $agent;
}
/**
*设置http方法,get,post
*@param
*/
public function setmethod($method) {
$this->method = $method;
}
/**
*设置url
*@param
*/
public function set {
$this->url = $this->host . $url;
}
/**
*@param
*@return
*/
public function setcookie($cookie) {
$this->cookie = "";
foreach($cookie as $key => $value) {
$this->cookie .= "{$key}={$value};";
}
}
/**
* 设置post参数
* @param array $postparm
* @return object
*/
public function setpostparam($postparam) {
$this->postparam = self::flattenarr($postparam);
return $this;
}
/**
* php://input
* @param string $input
* @return object
*/
public function setrawpostparam($input) {
$this->postparam = $input;
return $this;
}
/**
* 多维数组转为一维
*
* from
* array(
* 'list' => array(
* array('a' => b, 'c' => 'd')
* ),
* );
*
* to
* array('list[0][a]' => b, 'list[0][c]' => d)
*
* @param array $array
* @param string $prefix
* @return array
*/
private static function flattenarr($array, $prefix = '') {
$result = array();
foreach ($array as $key => $value) {
$key = $prefix ? $prefix.'['.$key.']' : $key;
if (is_array($value)) {
$result = self::flattenarr($value, $key);
} else {
$result[$key] = $value;
}
}
return $result;
}
/**
*设置files
*@param
*/
public function setfiles($files) {
$filedata = array();
foreach ($files as $key => $file) {
$filedata[$key] = implode(';', array(
'@'.realpath($file['tmp_name']),
'type='.$file['type'],
'filename='.$file['name'],
));
}
$this->postparam = array_merge($filedata, $this->postparam);
}
/**
*设置refer
*@param
*/
public function setreferer($referer) {
$this->referer = $referer;
}
/**
*设置headers
*@param
*/
public function setheaders($headers) {
$this->headers = $headers;
}
/**
*设置超时
*@param
*/
public function gettimeout($timeout) {
$this->timeout = $timeout;
}
/**
* 是否直接输出
*
* @param bool $isdirectoutput
* @return object
*/
public function setdirectoutput($isdirectoutput) {
$this->isdirectoutput = $isdirectoutput;
return $this;
}
/**
* 发送请求
* @param void
* @return mixed
*/
public function dorequest() {
$ch = curl_init();
curl_setopt($ch, curlopt_header, !$this->isdirectoutput);
curl_setopt($ch, curlopt_url, $this->url);
curl_setopt($ch, curlopt_encoding, "gzip");
curl_setopt($ch, curlopt_timeout, $this->timeout);
curl_setopt($ch, curlopt_followlocation, 1);
curl_setopt($ch, curlopt_returntransfer, 1);
if (!empty($this->agent)) {
curl_setopt($ch, curlopt_useragent,$this->agent);
}
if (!empty($this->postparam)) {
curl_setopt($ch, curlopt_post, 1);
curl_setopt($ch, curlopt_postfields, $this->postparam);
}
if (!empty($this->referer)) {
curl_setopt($ch, curlopt_referer , $this->referer);
}
if ($this->cookie != false){
curl_setopt($ch, curlopt_cookie, $this->cookie);
}
if (!empty($this->headers)) {
curl_setopt($ch, curlopt_httpheader, $this->headers);
}
if ($this->isdirectoutput) {
curl_setopt($ch, curlopt_headerfunction, array($this, 'readcurlheader'));
curl_setopt($ch, curlopt_writefunction, array($this, 'readcurldata'));
}
$response = curl_exec($ch);
if (curl_errno($ch)) {
return false;
}
$body = '';
if (!$this->isdirectoutput) {
$headersize = curl_getinfo($ch, curlinfo_header_size);
$this->responseheader = substr($response, 0, $headersize);
$this->showheaders();
$body = substr($response, $headersize);
}
curl_close($ch);
return $body;
}
/**
* @param object $curl
* @param string $data
* @return int
*/
private function readcurlheader($curl, $header) {
if (!self::isneedfilter($header)) {
header($header);
}
return strlen($header);
}
/**
* @param object $curl
* @param string $data
* @return int
*/
private function readcurldata($curl, $data) {
echo $data;
return strlen($data);
}
/**
* @brief 返回reponseheader
* @param
* @return
*/
public function getresponseheaders() {
return $this->responseheader;
}
/**
* @breif 绚烂输出的header
* @param
* @return
*/
public function showheaders() {
$arrheader = explode("\r\n", $this->responseheader);
foreach($arrheader as $header) {
if (empty($header) || self::isneedfilter($header)) {
continue;
}
header($header);
}
}
/**
* 是否需要过滤 header
*
* @param string $header
* @return bool
*/
private static function isneedfilter($header) {
static $headers = array(
'connection',
'content-encoding',
'date',
'server',
'set-cookie',
'tracecode',
'transfer-encoding',
'vary',
);
if (strpos($header, ':') === false) {
return false;
}
list($key, $value) = explode(':', $header);
if (in_array($key, $headers)) {
return true;
}
return false;
}
}
function curldispatch() {
//转发请求
$urlhelper = new proxy_http();
$url = 'http://test.com';
$_get['name'] = 'aa';
$_get['id'] = 'test';
if (!empty($_get)) {
$url .= "?" . http_build_query($_get);
}
$urlhelper->setheaders(array(
"id:1223",
"name:testname",
));
$urlhelper->set;
if (!empty($_post)) {
$urlhelper->setpostparam($_post);
}
if (!empty($_files)) {
$urlhelper->setfiles($_files);
}
if(!empty($_cookie)) {
$urlhelper->setcookie($_cookie);
}
$input = file_get_contents('php://input');
if (!empty($input) && empty($_post)) {
$urlhelper->setrawpostparam(urlencode($input));
}
// dorequest 直接输出内容
$needdirectoutput = array(
'file/download', // 文件下载
);
$controller = '';
$action = '';
//跟进请求的链接判断是否需要输出内容
if (in_array($controller.'/'.$action, $needdirectoutput)) {
$urlhelper->setdirectoutput(true);
}
$result = $urlhelper->dorequest();
echo $result;die;
}
curldispatch();
相关推荐
php curl 模拟提交(支持代理),一个封装好的功能能类,直接调用就好了
利用php中的 curl 请求api php支持的由daniel stenberg创建的libcurl库允许你与各种的服务器使用各种类型的协议进行连接和通讯。 libcurl目前支持http、https、ftp、gopher、telnet、dict、file和ldap协议。libcurl...
本文实例讲述了php使用curl通过代理获取数据的实现方法。分享给大家供大家参考,具体如下: $curl=curl_init(); curl_setopt($curl, curlopt_url, "http://www.baidu.com/"); curl_setopt($curl, curlopt_useragent...
class curl { private static $ins = null; private $body = null; private $cookiefile = null; private $method = array('get','post'); final private function __construct...
* 1.支持单个get,post请求 * 2.支持多个目标未登录get请求 * 3.支持单个目标并行多个get,post请求 * 4.... * 5.... * 6.... * 7.支持代理登陆 * 8.支持自定义来路 * 9.支持自定义超时 * 10.支持文件上传
本文实例讲述了php使用curl代理实现抓取数据的方法。分享给大家供大家参考,具体如下: <?php define ( 'is_proxy', true ); //是否启用代理 function async_get_ { if (!is_...
curl 模似http请求工具 支持以下功能: * 1:支持ssl连接和proxy代理连接 ...include('clsss/class.curl.php'); $cu = new c; //得到 baidu 的凯发k8国际首页内容 echo $cu->get('http://www.baidu.com');
libcurl 同时支持 https 证书、http post、http put、 ftp 上传(也能通过 php 的 ftp 扩展完成)、http 基于表单的上传、代理、cookies、用户名 密码的认证。 我们可以使用curl通过凯发k8国际的服务器去获取其他服务器的...
curl各种版的dll文件集合,使用php的curl可以实现支持ftp、ftps、http htpps scp sftp tftp telnet dict file和ldap。curl 支持ssl证书、http post、http put 、ftp 上传,kerberos、基于htt格式的上传、代理、cookie...
简单的 php-curl 库要求php 5.1 卷曲特征通过 http post/get/put/delete 请求http 认证跟随重定向返回错误字符串提供调试信息代理支持饼干下载例子 $curl = new curl;简单调用这些都在一行代码中完成,让生活变得...
官方解释curl是一个利用url语法在命令行方式下工作的文件传输工具。curl是一个利用url语法在命令行方式下工作的文件传输工具。它支持很多协议:ftp, ftps, http, https, gopher, telnet, dict, file 以及 ldap。curl...
php-proxy 是基于 php,symfony 和 curl 的代理脚本,这个库借鉴了 glype,jenssegers proxy 和 guzzle 的经验。示例require('vendor/autoload.php'); use proxy\http\request; use proxy\proxy; $request = ...
curlhttpclient 基于curl的http客户端-简单但有效的oop包装器,用于curl php lib。 它允许发送发布/获取请求,使用代理,绑定到特定ip,存储cookie等。安装将curlhttpclient添加到您的composer.json { "require": { ...
斑马curl 高性能的curl php库允许异步异步运行多个请求 zebra curl是一个高性能的curl php库,它不仅允许一次运行多个异步请求,而且可以立即处理完成的线程,而不必等待队列中的其他线程完成。 同样,每次完成一个...
一个基于php curl的开源http类库,支持链式操作,省去繁杂的curl使用方法,简单易用。 支持所有常见的get、post、put、delete、update等请求方式,支持设置和读取header、cookie、请求参数、失败重试、限速、代理、...
本项目完全开源,是php版调用openai的api接口进行问答的demo,有以下特性和功能: 对php版本无要求,不需要数据库。核心代码只有几个文件,没用任何框架,修改调试很方便。 采用stream流模式通信,一边生成一边输出...
如何使用将脚本复制到php web服务器的可公开访问的文件夹中(该脚本是独立的并且没有php依赖项) 发出针对此脚本的curl请求添加具有身份验证密钥的proxy-auth标头添加具有代理请求的url的proxy-target-url标头(可选...
curl是与各种的服务器使用各种类型的协议进行连接和通讯的工具。它是一个强大的库支持http、https、ftp、telnet、file等...文件的设置,找到php_curl.dll,取消下在的注释extension=php_curl.dll,因为php默认是不开
libcurl主要功能就是用不同的协议连接和沟通不同的服务器~也就是相当封装了的sockphp 支持libcurl(允许你用不同...php自带curl扩展,但java没有curl扩展,这个工程的目的,就是将libcurl进行封装,以jni的方式进行调用
alt d显示虚拟地址栏,esc去除地址栏。 需要curl的支持。 另外,,只支持firefox哈哈