PHP封装CURL扩展类实例

前端之家收集整理的这篇文章主要介绍了PHP封装CURL扩展类实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

@H_403_0@本文实例讲述了PHP封装CURL扩展类。分享给大家供大家参考。具体如下:


<div class="jb51code">
<pre class="brush:PHP;">
<?php
/**

  • @description: 封装CURL扩展
  • @date: 2014-07-28 16:04
    */
    /**
  • @编码规范
  • @class 类名首字母大写,类名为多个单词,每个大字首字母大写 eg: class Curl,class CurlPage
  • @variable 变量名小写,变量名为多个单词,每个单词小写,使用下划线_分割 eg: $curl_result
  • @function 函数名与类名规则相同 eg: function SendRequest
  • @params 函数形参规则与变量名相同
  • @class-variable 成员变量,以下划线结尾,多个单词使用下划线分隔. eg: private $hostname
    */
    /**
  • @要求
  • */
    class Curl{
    /**

  • @请求的host
    */
    private $host_;
    /**
  • @curl 句柄
    */
    private $ch_;
    /**
  • @超时限制时间
    */
    const time_=5;
    /**
  • @请求的设置
    */
    private $options_;
    /**
  • @保存请求头信息
    */
    private $requestheader;
    /**
  • @保存响应头信息
    */
    private $responseheader;
    /**
  • @body 用于保存curl请求返回的结果
    */
    private $body
    ;
    /**
  • @读取cookie
    */
    private $cookiefile;
    /**
  • @写入cookie
    */
    private $cookiejar;
    /**
  • @todo proxy
  • @构造函数,初始化CURL回话
    */
    public function Start($url){
    $this->ch_ = curl_init($url);
    curlsetopt($this->ch,CURLOPT_HEADER,1);
    curlsetopt($this->ch,CURLOPT_RETURNTRANSFER,1 );
    }
    /**
  • @返回响应头
    */
    public function ResponseHeader($url){
    if (!function_exists('http_parse_headers')) {
    function http_parse_headers ($raw_headers){
    $headers = array();
    foreach (explode("\n",$raw_headers) as $i => $h) {
    $h = explode(':',$h,2);
    if (isset($h[1])) {
    if(!isset($headers[$h[0]])) {
    $headers[$h[0]] = trim($h[1]);
    } else if(is_array($headers[$h[0]])) {
    $tmp = array_merge($headers[$h[0]],array(trim($h[1])));
    $headers[$h[0]] = $tmp;
    } else {
    $tmp = array_merge(array($headers[$h[0]]),array(trim($h[1])));
    $headers[$h[0]] = $tmp;
    }
    }
    }
    return $headers;
    }
    }
    $this->Start($url);
    curlsetopt($this->ch,CURLOPTTIMEOUT,Curl::time);
    $this->body_=$this->Execx();
    $header_size = curlgetinfo($this->ch,CURLINFO_HEADER_SIZE);
    $this->responseheader = substr($this->body_,$start = 0,$offset = $header_size);
    $this->responseheader = http_parse_headers($this->responseheader);
    print_r($this->responseheader);
    return $this->Close($this->body_);
    }
    /**
  • @读取cookie
    */
    public function LoadCookie($url,$cookie_file){
    $this->Start($url);
    curlsetopt($this->ch,CURLOPT_COOKIE,CURLOPT_COOKIEFILE,$cookiefile);
    $this->body
    =$this->Execx();
    return $this->Close($this->body_);
    }
    /**
  • @写入cookie
    */
    public function SaveCookie($url){
    $this->Start($url);
    curlsetopt($this->ch,'cookie.txt');
    curlsetopt($this->ch,CURLOPTCOOKIEJAR,'cookie.txt');
    $this->body
    =$this->Execx();
    return $this->Close($this->body_);
    }
    /**
  • @设置HEADER
    */
    public function SetHeader($headers = null){
    if (is_array($headers) && count($headers) > 0) {
    curlsetopt($this->ch,CURLOPT_HTTPHEADER,$headers);
    }
    }
    /**
  • @GET请求
    */
    public function Get($url,array $params = array()) {
    if ($params) {
    if (strpos($url,'?')) {
    $url .= "&".http_build_query($params);
    }
    else {
    $url .= "?".http_build_query($params);
    }
    }
    $this->Start($url);
    curlsetopt($this->ch,Curl::time_);
    if (strpos($url,'https') === 0) {
    curlsetopt($this->ch,CURLOPT_SSL_VERIFYHOST,0);
    curlsetopt($this->ch,CURLOPT_SSLVERIFYPEER,0);
    }
    $this->body
    =$this->Execx();
    return $this->Close($this->body_);
    }
    /**
  • @POST请求
    */
    public function Post($url,array $params = array()) {
    $this->Start($url);
    curlsetopt($this->ch,array("Content-Type: application/x-www-form-urlencoded"));
    curlsetopt($this->ch,CURLOPT_POST,true);
    curlsetopt($this->ch,Curl::time_);
    if ($params) {
    curlsetopt($this->ch,CURLOPT_POSTFIELDS,http_buildquery($params));
    }
    $this->body
    =$this->Execx();
    return $this->Close($this->body_);
    }
    /**
  • @tips: google http head 方法
    */
    public function Head($url,Curl::time_);
    curlsetopt($this->ch,0);
    curlsetOpt($this->ch,CURLOPTNOBODY,true);
    $this->body
    =$this->Execx();
    return $this->Close($this->body_);
    }
    /**
  • @执行CURL会话
    */
    public function Execx(){
    return curlexec($this->ch);
    }
    /**
  • @关闭CURL句柄
    */
    public function Close($body){
    if ($body
    === false) {
    echo "CURL Error: " . curlerror($body);
    return false;
    }
    curlclose($this->ch);
    return $body_;
    }
    }
@H_403_0@希望本文所述对大家的PHP程序设计有所帮助。

原文链接:https://www.f2er.com/php/21423.html

猜你在找的PHP相关文章