php實現的Curl封裝類Curl.class.php用法執行個體分析_php技巧

來源:互聯網
上載者:User

本文執行個體講述了php實現的Curl封裝類Curl.class.php用法。分享給大家供大家參考。具體如下:

<?php//curl類class Curl{ function Curl(){  return true; } function execute($method, $url, $fields='', $userAgent='', $httpHeaders='', $username='', $password=''){  $ch = Curl::create();  if(false === $ch){   return false;  }  if(is_string($url) && strlen($url)){   $ret = curl_setopt($ch, CURLOPT_URL, $url);  }else{   return false;  }  //是否顯示頭部資訊  curl_setopt($ch, CURLOPT_HEADER, false);  //  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  if($username != ''){   curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);  }  $method = strtolower($method);  if('post' == $method){   curl_setopt($ch, CURLOPT_POST, true);   if(is_array($fields)){    $sets = array();    foreach ($fields AS $key => $val){     $sets[] = $key . '=' . urlencode($val);    }    $fields = implode('&',$sets);   }   curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);  }else if('put' == $method){   curl_setopt($ch, CURLOPT_PUT, true);  }  //curl_setopt($ch, CURLOPT_PROGRESS, true);  //curl_setopt($ch, CURLOPT_VERBOSE, true);  //curl_setopt($ch, CURLOPT_MUTE, false);  curl_setopt($ch, CURLOPT_TIMEOUT, 10);//設定curl逾時秒數  if(strlen($userAgent)){   curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);  }  if(is_array($httpHeaders)){   curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);  }  $ret = curl_exec($ch);  if(curl_errno($ch)){   curl_close($ch);   return array(curl_error($ch), curl_errno($ch));  }else{   curl_close($ch);   if(!is_string($ret) || !strlen($ret)){    return false;   }   return $ret;  } } function post($url, $fields, $userAgent = '', $httpHeaders = '', $username = '', $password = ''){  $ret = Curl::execute('POST', $url, $fields, $userAgent, $httpHeaders, $username, $password);  if(false === $ret){   return false;  }  if(is_array($ret)){   return false;  }  return $ret; } function get($url, $userAgent = '', $httpHeaders = '', $username = '', $password = ''){  $ret = Curl::execute('GET', $url, '', $userAgent, $httpHeaders, $username, $password);  if(false === $ret){   return false;  }  if(is_array($ret)){   return false;  }  return $ret; } function create(){  $ch = null;  if(!function_exists('curl_init')){   return false;  }  $ch = curl_init();  if(!is_resource($ch)){   return false;  }  return $ch; }}?>

GET用法:

$curl = new Curl();$curl->get('http://www.XXX.com/');

POST用法:

$curl = new Curl();$curl->get('http://www.XXX.com/', 'p=1&time=0');

希望本文所述對大家的php程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.