国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php實現httpclient類示例

2024-05-04 23:22:47
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:


httpClient::init($httpClient, $args = null);
$httpClient->get($url, $data = null, $cookie = null);
var_dump($httpClient->buffer);

復制代碼 代碼如下:


<?php

class httpClient {

 public $buffer = null;  // buffer 獲取返回的字符串
 public $referer = null;  // referer 設置 HTTP_REFERER 的網址
 public $response = null; // response 服務器響應的 header 信息
 public $request = null;  // request 發送到服務器的 header 信息
 private $args = null;

 public static function init(&$instanceof, $args = array()) {
  return $instanceof = new self($args);
 }

 private function __construct($args = array()) {

  if(!is_array($args)) $args = array();
  $this->args = $args;
  if(!empty($this->args['debugging'])) {
   ob_end_clean();
   set_time_limit(0);
   header('Content-Type: text/plain; charset=utf-8');
  }

 }

 public function get($url, $data = null, $cookie = null) {

  $parse = parse_url($url);
  $url .= isset($parse['query']) ? '&'. $data : ( $data ? '?'. $data : '' );
  $host = $parse['host'];

  $header  = 'Host: '. $host. "/r/n";
  $header .= 'Connection: close'. "/r/n";
  $header .= 'Accept: */*'. "/r/n";
  $header .= 'User-Agent: '. ( isset($this->args['userAgent']) ? $this->args['userAgent'] : $_SERVER['HTTP_USER_AGENT'] ). "/r/n";
  $header .= 'DNT: 1'. "/r/n";
  if($cookie) $header .= 'Cookie: '. $cookie. "/r/n";
  if($this->referer) $header .= 'Referer: '. $this->referer. "/r/n";

  $options = array();
  $options['http']['method'] = 'GET';
  $options['http']['header'] = $header;

  $response = get_headers($url);
  $this->request = $header;
  $this->response = implode("/r/n", $response);
  $context = stream_context_create($options);
  return $this->buffer = file_get_contents($url, false, $context);

 }

 public function post($url, $data = null, $cookie = null) {

  $parse = parse_url($url);
  $host = $parse['host'];

  $header  = 'Host: '. $host. "/r/n";
  $header .= 'Connection: close'. "/r/n";
  $header .= 'Accept: */*'. "/r/n";
  $header .= 'User-Agent: '. ( isset($this->args['userAgent']) ? $this->args['userAgent'] : $_SERVER['HTTP_USER_AGENT'] ). "/r/n";
  $header .= 'Content-Type: application/x-www-form-urlencoded'. "/r/n";
  $header .= 'DNT: 1'. "/r/n";
  if($cookie) $header .= 'Cookie: '. $cookie. "/r/n";
  if($this->referer) $header .= 'Referer: '. $this->referer. "/r/n";
  if($data) $header .= 'Content-Length: '. strlen($data). "/r/n";

  $options = array();
  $options['http']['method'] = 'POST';
  $options['http']['header'] = $header;
  if($data) $options['http']['content'] = $data;

  $response = get_headers($url);
  $this->request = $header;
  $this->response = implode("/r/n", $response);
  $context = stream_context_create($options);
  return $this->buffer = file_get_contents($url, false, $context);

 }

}

httpClient::init($httpClient, array( 'debugging' => true , 'userAgent' => 'MSIE 15.0' ));
$httpClient->get('http://www.baidu.com', 'name=haowei');
echo $httpClient->request; // 獲取 請求頭部信息
echo $httpClient->response; // 獲取 響應的頭部信息
echo $httpClient->buffer; // 獲取 網頁內容

$httpClient->get('http://m.survivalescaperooms.com/ServiceLogin/', 'hash='. $time, 'uid=1;users=admin;')

echo $httpClient->buffer;

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 化德县| 锦屏县| 通州市| 江源县| 定日县| 岱山县| 大余县| 阳春市| 溆浦县| 兴宁市| 焦作市| 巴彦淖尔市| 阿鲁科尔沁旗| 海兴县| 察哈| 舒城县| 武宣县| 正安县| 兰州市| 阿拉善右旗| 沅江市| 内江市| 西充县| 临夏市| 大名县| 吉木乃县| 新化县| 磐安县| 改则县| 休宁县| 万盛区| 牡丹江市| 视频| 奉节县| 闻喜县| 南充市| 九寨沟县| 北碚区| 榆树市| 新河县| 炉霍县|