php实现的Curl封装类Curl.class.php用法实例分析

yipeiwu_com5年前PHP代码库

本文实例讲述了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程序设计有所帮助。

相关文章

php实现文件下载更能介绍

PHP用代码实现文件下载,阅读PHP用代码实现文件下载,我们一般实现下载都是调用url来下载,但是遇到ie能识别打开的文件就不能用这种方式了,比如下载一个图片、html网页等,这时就需要...

PHP实现的进度条效果详解

PHP实现的进度条效果详解

本文实例讲述了PHP实现的进度条效果。分享给大家供大家参考,具体如下: 在做采集的时候,想通过php来实现一个进度条功能,谷歌了一下,找了个合适的代码。下面直接上代码: <&...

PHP使用pear实现mail发送功能 windows环境下配置pear

PHP使用pear实现mail发送功能 windows环境下配置pear

PHP发邮件可以用其自带mail()函数,但是这个函数很不好使,需要配置邮件服务器,并且不支持smtp验证,在很多场合无法正常的工作. 找了个代码发邮件,但总是出错,我在这里用PEAR的...

PHP 数据结构 算法描述 冒泡排序 bubble sort

复制代码 代码如下: <?php /** * 冒泡排序 bubble sort * * 原理:多次循环进行比较,每次比较时将最大数移动到最上面。每次循环时,找出剩余变量里的最大值,...

Discuz不使用插件实现简单的打赏功能

Discuz不使用插件实现简单的打赏功能

实现目标:用户自行上传自己的支付宝及微信收款二维码,在主题帖增加打赏按钮及浮窗。 功能逻辑:利用后台自定义用户栏目实现用户上传二维码的功能,然后再在内容页加以判断、调用。 修改步骤: 1...