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


Posted in PHP onSeptember 25, 2015

本文实例讲述了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 编程请选择正确的文本编辑软件
Dec 21 PHP
ASP和PHP都是可以删除自身的
Apr 09 PHP
php面向对象全攻略 (十一)__toString()用法 克隆对象 __call处理调用错误
Sep 30 PHP
用PHP实现浏览器点击下载TXT文档的方法详解
Jun 02 PHP
thinkphp缓存技术详解
Dec 09 PHP
帝国cms常用标签汇总
Jul 06 PHP
PHP+Mysql+jQuery查询和列表框选择操作实例讲解
Oct 22 PHP
Yii2实现ajax上传图片插件用法
Apr 28 PHP
Yii2使用$this-&gt;context获取当前的Module、Controller(控制器)、Action等
Mar 29 PHP
Yii框架分页实现方法详解
May 20 PHP
laravel5.0在linux下解决.htaccess无效和去除index.php的问题
Oct 16 PHP
PHP For循环字母A-Z当超过26个字母时输出AA,AB,AC
Feb 16 PHP
php实现的微信红包算法分析(非官方)
Sep 25 #PHP
PHP简单实现断点续传下载的方法
Sep 25 #PHP
分享3个php获取日历的函数
Sep 25 #PHP
PHP中配置IIS7实现基本身份验证的方法
Sep 24 #PHP
常见PHP数据库解决方案分析介绍
Sep 24 #PHP
又十个超级有用的PHP代码片段
Sep 24 #PHP
PHP易混淆知识整理笔记
Sep 24 #PHP
You might like
ajax php 实现写入数据库
2009/09/02 PHP
解析thinkphp中的M()与D()方法的区别
2013/06/22 PHP
php从字符串创建函数的方法
2015/03/16 PHP
详解php比较操作符的安全问题
2015/12/03 PHP
Yii1.1中通过Sql查询进行的分页操作方法
2017/03/16 PHP
jQuery 点击图片跳转上一张或下一张功能的实现代码
2010/03/12 Javascript
基于jQuery的获得各种控件Value的方法
2010/11/19 Javascript
JS弹出对话框实现方法(三种方式)
2015/12/18 Javascript
js简单判断移动端系统的方法
2016/02/25 Javascript
基于vue的下拉刷新指令和滚动刷新指令
2016/12/23 Javascript
JavaScript中click和onclick本质区别与用法分析
2018/06/07 Javascript
vue.js使用v-if实现显示与隐藏功能示例
2018/07/06 Javascript
图文详解vue框架安装步骤
2019/02/12 Javascript
vue基础之v-bind属性、class和style用法分析
2019/03/11 Javascript
layui数据表格跨行自动合并的例子
2019/09/02 Javascript
jQuery实现简单评论功能
2020/08/19 jQuery
由Python运算π的值深入Python中科学计算的实现
2015/04/17 Python
python编写微信远程控制电脑的程序
2018/01/05 Python
python入门教程 python入门神图一张
2018/03/05 Python
Python快速转换numpy数组中Nan和Inf的方法实例说明
2019/02/21 Python
聊聊python在linux下与windows下导入模块的区别说明
2021/03/03 Python
美国宠物商店:Wag.com
2016/10/25 全球购物
瑞典领先的汽车零部件网上零售商:bildelaronline24.se
2017/01/12 全球购物
Marlies Dekkers内衣法国官方网上商店:国际知名的荷兰内衣品牌
2019/03/18 全球购物
中东地区最大的奢侈品市场:The Luxury Closet
2019/04/09 全球购物
Big Green Smile法国:领先的英国有机和天然产品在线商店
2021/01/02 全球购物
北京SQL新华信咨询
2016/09/30 面试题
党员实事承诺书
2014/03/26 职场文书
事业单位竞聘上岗实施方案
2014/03/28 职场文书
体育系毕业生求职自荐信
2014/04/16 职场文书
励志演讲稿500字
2014/08/21 职场文书
刑事代理授权委托书
2014/09/17 职场文书
见习报告的格式
2014/10/31 职场文书
刮痧观后感
2015/06/05 职场文书
关于保护环境的建议书
2019/06/24 职场文书
Python读取文件夹下的所有文件实例代码
2021/04/02 Python