php實現(xiàn)的Curl封裝類Curl.class.php用法實例分析
更新時間:2015年09月25日 11:18:30 作者:deeka
這篇文章主要介紹了php實現(xiàn)的Curl封裝類Curl.class.php用法,以完整實例形式較為詳細(xì)的分析了Curl封裝類的定義及相關(guān)使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了php實現(xiàn)的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);//設(shè)置curl超時秒數(shù)
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程序設(shè)計有所幫助。
相關(guān)文章
phpmyadmin安裝時提示:Warning: require_once(./libraries/common.inc
common.inc.php這個文件是存在的,而且系統(tǒng)中php也不裝在C盤根目錄下,怎么會有這樣的提示呢?經(jīng)過一陣調(diào)試終于發(fā)現(xiàn)解決方法2011-08-08
PHP5.2中date()函數(shù)顯示時間與北京時間相差8小時的解決辦法
北京時間是東8區(qū),可能是時區(qū)問題.但是此前都沒問題啊,經(jīng)過google發(fā)現(xiàn)原來從php5.1.0開始,php.ini里加入了 date.timezone這個選項,默認(rèn)情況下是關(guān)閉的2009-05-05
PHP實現(xiàn)截取中文字符串不出現(xiàn)?號的解決方法
這篇文章主要介紹了PHP實現(xiàn)截取中文字符串不出現(xiàn)?號的解決方法,涉及php字符串遍歷及編碼轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2016-12-12

