《PHP實(shí)例:php實(shí)現(xiàn)的Curl封裝類Curl.class.php用法實(shí)例分析》要點(diǎn):
本文介紹了PHP實(shí)例:php實(shí)現(xiàn)的Curl封裝類Curl.class.php用法實(shí)例分析,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
PHP編程本文實(shí)例講述了php實(shí)現(xiàn)的Curl封裝類Curl.class.php用法.分享給大家供大家參考.具體如下:
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;
}
}
?>
PHP編程GET用法:
PHP編程
$curl = new Curl();
$curl->get('http://www.XXX.com/');
PHP編程POST用法:
PHP編程
$curl = new Curl();
$curl->get('http://www.XXX.com/', 'p=1&time=0');
PHP編程希望本文所述對(duì)大家的php程序設(shè)計(jì)有所贊助.
維易PHP培訓(xùn)學(xué)院每天發(fā)布《PHP實(shí)例:php實(shí)現(xiàn)的Curl封裝類Curl.class.php用法實(shí)例分析》等實(shí)戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/8701.html