php實現httpclient類樣本_PHP教程

來源:互聯網
上載者:User
複製代碼 代碼如下:
httpClient::init($httpClient, $args = null);
$httpClient->get($url, $data = null, $cookie = null);
var_dump($httpClient->buffer);

複製代碼 代碼如下:

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://www.jb51.net/ServiceLogin/', 'hash='. $time, 'uid=1;users=admin;')

echo $httpClient->buffer;

http://www.bkjia.com/PHPjc/750861.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/750861.htmlTechArticle複製代碼 代碼如下: httpClient::init($httpClient, $args = null); $httpClient-get($url, $data = null, $cookie = null); var_dump($httpClient-buffer); 複製代碼 代碼如下...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.