php實(shí)現(xiàn)的Curl封裝類Curl.class.php用法實(shí)例分析
更新時(shí)間:2015年09月25日 11:18:30 作者:deeka
這篇文章主要介紹了php實(shí)現(xiàn)的Curl封裝類Curl.class.php用法,以完整實(shí)例形式較為詳細(xì)的分析了Curl封裝類的定義及相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了php實(shí)現(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í)秒數(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');
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP實(shí)現(xiàn)基于文本的簡(jiǎn)易搜索引擎功能
這篇文章給大家介紹了PHP實(shí)現(xiàn)基于文本的簡(jiǎn)易搜索引擎功能,讓這個(gè)功能可以在小型網(wǎng)站或者特定數(shù)據(jù)集內(nèi)提供快速的關(guān)鍵字搜索能力,非常適合沒有使用復(fù)雜數(shù)據(jù)庫(kù)搜索引擎(如Elasticsearch)的場(chǎng)景,需要的朋友可以參考下2024-02-02PHP調(diào)用Linux的命令行執(zhí)行文件壓縮命令
一開始,我和普通青年一樣,想到用PHP內(nèi)置的 ZipArchive糾結(jié)的是環(huán)境上沒安裝zip擴(kuò)展,想采用用PHP調(diào)用Linux的命令行 ,執(zhí)行壓縮命令,感興趣的朋友可以了解下,希望本文對(duì)你有所幫助2013-01-01PHP多維數(shù)組遍歷方法(2種實(shí)現(xiàn)方法)
這篇文章主要介紹了PHP多維數(shù)組遍歷方法,實(shí)例分析了2種多維數(shù)組的遍歷技巧,包括簡(jiǎn)單的foreach遍歷與遞歸操作遍歷實(shí)現(xiàn)方法,需要的朋友可以參考下2015-12-12php下判斷數(shù)組中是否存在相同的值array_unique
今天在改一個(gè)N久以前寫的程序 突然碰到一個(gè)問(wèn)題 假設(shè)有一個(gè)數(shù)組$a中存在幾個(gè)value 我如何判斷這些value當(dāng)中是否存在相同的值呢? 翻了好多資料,也問(wèn)了兵哥哥,給我一些思路,想自己寫來(lái)著~~~ 還是不肯放棄百度,最后搞了一次,居然找到這么一個(gè)函數(shù) array_unique爽大了。2008-03-03PHP開發(fā)中AJAX技術(shù)的簡(jiǎn)單應(yīng)用
這篇文章主要介紹了PHP開發(fā)中AJAX技術(shù)的簡(jiǎn)單應(yīng)用,簡(jiǎn)單對(duì)ajax的執(zhí)行原理、實(shí)際應(yīng)用作介紹,感興趣的小伙伴們可以參考一下2015-12-12