PHP 兼容 Curl/Socket/Stream 的 HTTP 操作类
来源:互联网 发布:如何自学中医 知乎 编辑:程序博客网 时间:2024/04/29 02:22
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class Http
- {
-
-
-
- const TYPE_CURL = 1;
-
-
-
- const TYPE_SOCK = 2;
-
-
-
- const TYPE_STREAM = 3;
-
-
-
-
-
- private function __clone() {}
-
-
-
-
- private function __construct() {}
-
-
-
-
-
-
-
-
-
- public static function factory($url = '', $type = self::TYPE_SOCK){
- if ($type == ''){
- $type = self::TYPE_SOCK;
- }
- switch($type) {
- case self::TYPE_CURL :
- if (!function_exists('curl_init')){
- throw new Exception(__CLASS__ . " PHP CURL extension not install");
- }
- $obj = Http_Curl::getInstance($url);
- break;
- case self::TYPE_SOCK :
- if (!function_exists('fsockopen')){
- throw new Exception(__CLASS__ . " PHP function fsockopen() not support");
- }
- $obj = Http_Sock::getInstance($url);
- break;
- case self::TYPE_STREAM :
- if (!function_exists('stream_context_create')){
- throw new Exception(__CLASS__ . " PHP Stream extension not install");
- }
- $obj = Http_Stream::getInstance($url);
- break;
- default:
- throw new Exception("http access type $type not support");
- }
- return $obj;
- }
-
-
-
-
-
-
-
-
-
- public static function makeQuery($data, $sep = '&'){
- $encoded = '';
- while (list($k,$v) = each($data)) {
- $encoded .= ($encoded ? "$sep" : "");
- $encoded .= rawurlencode($k)."=".rawurlencode($v);
- }
- return $encoded;
- }
-
- }
-
-
-
-
-
-
-
- class Http_Curl
- {
-
-
-
- static $_instance = NULL;
-
-
-
-
- private $cookies = '';
-
-
-
- private $header = array();
-
-
-
- private $uri = '';
-
-
-
- private $vars = array();
-
-
-
-
-
-
- private function __construct($url){
- $this->uri = $url;
- }
-
-
-
-
- private function __clone() {}
-
-
-
-
-
-
-
-
- public static function getInstance($url = ''){
- if (!(self::$_instance instanceof self)){
- self::$_instance = new self($url);
- }
- return self::$_instance;
- }
-
-
-
-
-
-
-
-
- public function setHeader($header){
- if (emptyempty($header)) {
- return;
- }
- if (is_array($header)){
- foreach ($header as $k => $v){
- $this->header[] = is_numeric($k) ? trim($v) : (trim($k) .": ". trim($v));
- }
- } elseif (is_string($header)){
- $this->header[] = $header;
- }
- }
-
-
-
-
-
-
-
-
-
-
- public function setCookie($cookie){
- if (emptyempty($cookie)) {
- return;
- }
- if (is_array($cookie)){
- $this->cookies = Http::makeQuery($cookie, ';');
- } elseif (is_string($cookie)){
- $this->cookies = $cookie;
- }
- }
-
-
-
-
-
-
-
-
-
- public function setVar($vars){
- if (emptyempty($vars)) {
- return;
- }
- if (is_array($vars)){
- $this->vars = $vars;
- }
- }
-
-
-
-
-
-
-
- public function setUrl($url){
- if ($url != '') {
- $this->uri = $url;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public function get($url = '', $vars = array(), $header = array(), $cookie = '', $timeout = 5, $options = array()){
- $this->setUrl($url);
- $this->setHeader($header);
- $this->setCookie($cookie);
- $this->setVar($vars);
- return $this->send('GET', $timeout);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public function post($url = '', $vars = array(), $header = array(), $cookie = '', $timeout = 5, $options = array()){
- $this->setUrl($url);
- $this->setHeader($header);
- $this->setCookie($cookie);
- $this->setVar($vars);
- return $this->send('POST', $timeout);
- }
-
-
-
-
-
-
-
-
-
-
- public function send($method = 'GET', $timeout = 5, $options = array()){
-
- if ($this->uri == ''){
- throw new Exception(__CLASS__ .": Access url is empty");
- }
-
-
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
-
-
- if (!emptyempty($options)){
- curl_setopt_array($ch , $options);
- }
-
- if ($method == 'GET' && !emptyempty($this->vars)){
- $query = Http::makeQuery($this->vars);
- $parse = parse_url($this->uri);
- $sep = isset($parse['query']) ? '&' : '?';
- $this->uri .= $sep . $query;
- }
-
- if ($method == 'POST'){
- curl_setopt($ch, CURLOPT_POST, 1 );
- curl_setopt($ch, CURLOPT_POSTFIELDS, $this->vars);
- }
-
-
- if (!emptyempty($this->cookies)){
- curl_setopt($ch, CURLOPT_COOKIE, $this->cookies);
- }
-
- if (emptyempty($this->header)){
- $this->header = array(
- 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1)',
-
-
- );
- }
- curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
-
-
- curl_setopt($ch, CURLOPT_URL, $this->uri);
- $data = curl_exec($ch);
- if( ($err = curl_error($ch)) ){
- curl_close($ch);
- throw new Exception(__CLASS__ ." error: ". $err);
- }
- curl_close($ch);
- return $data;
- }
-
-
-
- }
-
-
-
-
-
-
-
-
- class Http_Sock
- {
-
-
-
- static $_instance = NULL;
-
-
-
-
- private $cookies = '';
-
-
-
- private $header = array();
-
-
-
- private $uri = '';
-
-
-
- private $vars = array();
-
-
-
-
-
-
- private function __construct($url){
- $this->uri = $url;
- }
-
-
-
-
- private function __clone() {}
-
-
-
-
-
-
-
-
- public static function getInstance($url = ''){
- if (!(self::$_instance instanceof self)){
- self::$_instance = new self($url);
- }
- return self::$_instance;
- }
-
-
-
-
-
-
-
-
- public function setHeader($header){
- if (emptyempty($header)) {
- return;
- }
- if (is_array($header)){
- foreach ($header as $k => $v){
- $this->header[] = is_numeric($k) ? trim($v) : (trim($k) .": ". trim($v));
- }
- } elseif (is_string($header)){
- $this->header[] = $header;
- }
- }
-
-
-
-
-
-
-
-
-
-
- public function setCookie($cookie){
- if (emptyempty($cookie)) {
- return;
- }
- if (is_array($cookie)){
- $this->cookies = Http::makeQuery($cookie, ';');
- } elseif (is_string($cookie)){
- $this->cookies = $cookie;
- }
- }
-
-
-
-
-
-
-
-
-
- public function setVar($vars){
- if (emptyempty($vars)) {
- return;
- }
- if (is_array($vars)){
- $this->vars = $vars;
- }
- }
-
-
-
-
-
-
-
- public function setUrl($url){
- if ($url != '') {
- $this->uri = $url;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public function get($url = '', $vars = array(), $header = array(), $cookie = '', $timeout = 5, $options = array()){
- $this->setUrl($url);
- $this->setHeader($header);
- $this->setCookie($cookie);
- $this->setVar($vars);
- return $this->send('GET', $timeout);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public function post($url = '', $vars = array(), $header = array(), $cookie = '', $timeout = 5, $options = array()){
- $this->setUrl($url);
- $this->setHeader($header);
- $this->setCookie($cookie);
- $this->setVar($vars);
- return $this->send('POST', $timeout);
- }
-
-
-
-
-
-
-
-
-
-
- public function send($method = 'GET', $timeout = 5, $options = array()){
-
- if ($this->uri == ''){
- throw new Exception(__CLASS__ .": Access url is empty");
- }
-
-
- if ($method == 'GET' && !emptyempty($this->vars)){
- $query = Http::makeQuery($this->vars);
- $parse = parse_url($this->uri);
- $sep = isset($parse['query'])&&($parse['query']!='') ? '&' : '?';
- $this->uri .= $sep . $query;
- }
-
-
- $data = '';
- if ($method == 'POST' && !emptyempty($this->vars)){
- $data = Http::makeQuery($this->vars);
- $this->setHeader('Content-Type: application/x-www-form-urlencoded');
- $this->setHeader('Content-Length: '. strlen($data));
- }
-
-
- $url = parse_url($this->uri);
- $host = $url['host'];
- $port = isset($url['port']) && ($url['port']!='') ? $url['port'] : 80;
- $path = isset($url['path']) && ($url['path']!='') ? $url['path'] : '/';
- $path .= isset($url['query']) ? "?". $url['query'] : '';
-
-
- array_unshift(&$this->header, $method ." ". $path ." HTTP/1.1");
- $this->setHeader('User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1)');
- if (!preg_match("/^[/d]{1,3}/.[/d]{1,3}/.[/d]{1,3}/.[/d]{1,3}$/", $host)){
- $this->setHeader("Host: ".$host);
- }
- if ($this->cookies != ''){
- $this->setHeader("Cookie: ". $this->cookies);
- }
- $this->setHeader("Connection: Close");
-
-
-
-
- $header = '';
- foreach ($this->header as $h) {
- $header .= $h ."/r/n";
- }
- $header .= "/r/n";
- if ($method == 'POST' && $data != ''){
- $header .= $data ."/r/n";
- }
-
-
- $ip = gethostbyname($host);
- if (!($fp = fsockopen($ip, $port, &$errno, &$errstr, $timeout))){
- throw new Exception(__CLASS__ .": Can't connect $host:$port, errno:$errno,message:$errstr");
- }
- fputs($fp, $header);
- $lineSize = 1024;
-
-
- $line = fgets($fp, $lineSize);
- $first = preg_split("//s/", trim($line));
- if ( isset($first[1]) && in_array($first[1], array('301','302')) ){
- while (!feof($fp)) {
- $line = fgets($fp, $lineSize);
- $second = preg_split("//s/", trim($line));
- if (ucfirst(trim($second[0]))=='Location:' && $second[1]!=''){
- $this->header = array();
- return $this->get(trim($second[1]));
- }
- }
- }
-
-
- $buf = '';
- $inheader = 1;
- while (!feof($fp)) {
- if ($inheader && ($line == "/n" || $line == "/r/n")) {
- $inheader = 0;
- }
- $line = fgets($fp, $lineSize);
- if ($inheader == 0) {
- $buf .=$line;
- }
- }
- fclose($fp);
-
- return $buf;
- }
-
- }
-
-
-
-
-
-
-
-
- class Http_Stream
- {
-
-
-
-
- static $_instance = NULL;
-
-
-
-
- private $cookies = '';
-
-
-
- private $header = array();
-
-
-
- private $uri = '';
-
-
-
- private $vars = array();
-
-
-
-
-
-
- private function __construct($url){
- $this->uri = $url;
- }
-
-
-
-
- private function __clone() {}
-
-
-
-
-
-
-
-
- public static function getInstance($url = ''){
- if (!(self::$_instance instanceof self)){
- self::$_instance = new self($url);
- }
- return self::$_instance;
- }
-
-
-
-
-
-
-
-
- public function setHeader($header){
- if (emptyempty($header)) {
- return;
- }
- if (is_array($header)){
- foreach ($header as $k => $v){
- $this->header[] = is_numeric($k) ? trim($v) : (trim($k) .": ". trim($v));
- }
- } elseif (is_string($header)){
- $this->header[] = $header;
- }
- }
-
-
-
-
-
-
-
-
-
-
- public function setCookie($cookie){
- if (emptyempty($cookie)) {
- return;
- }
- if (is_array($cookie)){
- $this->cookies = Http::makeQuery($cookie, ';');
- } elseif (is_string($cookie)){
- $this->cookies = $cookie;
- }
- }
-
-
-
-
-
-
-
-
-
- public function setVar($vars){
- if (emptyempty($vars)) {
- return;
- }
- if (is_array($vars)){
- $this->vars = $vars;
- }
- }
-
-
-
-
-
-
-
- public function setUrl($url){
- if ($url != '') {
- $this->uri = $url;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public function get($url = '', $vars = array(), $header = array(), $cookie = '', $timeout = 5, $options = array()){
- $this->setUrl($url);
- $this->setHeader($header);
- $this->setCookie($cookie);
- $this->setVar($vars);
- return $this->send('GET', $timeout);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public function post($url = '', $vars = array(), $header = array(), $cookie = '', $timeout = 5, $options = array()){
- $this->setUrl($url);
- $this->setHeader($header);
- $this->setCookie($cookie);
- $this->setVar($vars);
- return $this->send('POST', $timeout);
- }
-
-
-
-
-
-
-
-
-
-
- public function send($method = 'GET', $timeout = 5, $options = array()){
-
- if ($this->uri == ''){
- throw new Exception(__CLASS__ .": Access url is empty");
- }
- $parse = parse_url($this->uri);
- $host = $parse['host'];
-
-
- if ($method == 'GET' && !emptyempty($this->vars)){
- $query = Http::makeQuery($this->vars);
- $sep = isset($parse['query'])&&($parse['query']!='') ? '&' : '?';
- $this->uri .= $sep . $query;
- }
-
-
-
- $data = '';
- if ($method == 'POST' && !emptyempty($this->vars)){
- $data = Http::makeQuery($this->vars);
- }
-
-
- $this->setHeader('User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1)');
- if (!preg_match("/^[/d]{1,3}/.[/d]{1,3}/.[/d]{1,3}/.[/d]{1,3}$/", $host)){
- $this->setHeader("Host: ".$host);
- }
- if ($this->cookies != ''){
- $this->setHeader("Cookie: ". $this->cookies);
- }
- $this->setHeader("Connection: Close");
-
-
-
-
- $opts = array(
- 'http' => array(
- 'method' => $method,
- 'timeout' => $timeout,
- )
- );
- if ($data != ''){
- $opts['http']['content'] = $data;
- }
- $opts['http']['header'] = '';
- foreach ($this->header as $h){
- $opts['http']['header'] .= $h . "/r/n";
- }
-
-
-
- if (!emptyempty($options)){
- isset($options['proxy']) ? $opts['http']['proxy'] = $options['proxy'] : '';
- isset($options['max_redirects']) ? $opts['http']['max_redirects'] = $options['max_redirects'] : '';
- isset($options['request_fulluri']) ? $opts['http']['request_fulluri'] = $options['request_fulluri'] : '';
- }
-
-
- $context = stream_context_create($opts);
- if (($buf = file_get_contents($this->uri, null, $context)) === false){
- throw new Exception(__CLASS__ .": file_get_contents(". $this->uri .") fail");
- }
-
- return $buf;
-
- }
-
- }