PHP封装curl的调用接口及常用函数详解


Posted in PHP onMay 31, 2018

如下所示:

<?php
	/**
	 * @desc 封装curl的调用接口,post的请求方式
	 */
	function doCurlPostRequest($url, $requestString, $timeout = 5) { 
		if($url == "" || $requestString == "" || $timeout <= 0){
			return false;
		}
		 $con = curl_init((string)$url);
	 curl_setopt($con, CURLOPT_HEADER, false);
	 curl_setopt($con, CURLOPT_POSTFIELDS, $requestString);
	 curl_setopt($con, CURLOPT_POST, true);
	 curl_setopt($con, CURLOPT_RETURNTRANSFER,true);
	 curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout);
		curl_setopt($con, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($con, CURLOPT_SSL_VERIFYHOST, false);
	 return curl_exec($con);
	}
	
	/**
	 * @desc 封装curl的调用接口,post的请求方式
	 */
	function doCurlPostsslRequest($url, $requestString,$pemfile, $timeout = 5) { 
		if($url == "" || $requestString == "" || $timeout <= 0){
			return false;
		}
		
		echo $pemfile;
		 $con = curl_init((string)$url);
	 curl_setopt($con, CURLOPT_HEADER, false);
	 curl_setopt($con, CURLOPT_POSTFIELDS, $requestString);
	 curl_setopt($con, CURLOPT_POST, true);
	 curl_setopt($con, CURLOPT_RETURNTRANSFER,true);
	 curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout);
		curl_setopt($con, CURLOPT_SSL_VERIFYPEER, 1);
		curl_setopt($con, CURLOPT_SSL_VERIFYHOST, 2);
		//curl_setopt($ch, CURLOPT_SSLCERT, $pemfile); 
		//curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
		curl_setopt($con, CURLOPT_CAINFO, $pemfile);
	 return curl_exec($con);
	} 
	/**
	 * @desc 封装curl的调用接口,post Json数据的请求方式
	 */
	function doCurlPostJsonReq($url, $requestString, $timeout = 25){
		if($url == "" || $requestString == "" || $timeout <= 0){
			return false;
		}
	 $con = curl_init((string)$url);
	 curl_setopt($con, CURLOPT_HEADER, false);
	 curl_setopt($con, CURLOPT_POSTFIELDS, $requestString);
	 curl_setopt($con, CURLOPT_POST, true);
	 curl_setopt($con, CURLOPT_RETURNTRANSFER,true);
	 curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout);	
		curl_setopt($con, CURLOPT_HTTPHEADER, array(
			'Content-Type: application/json',
			'Content-Length: ' . strlen($data_string))
		);
	 return curl_exec($con);
	}
	/**
	 * @desc 封装curl的调用接口,post form数据的请求方式
	 */
	function doCurlPostFormReq($url, $requestString, $timeout = 25){
		if($url == "" || $requestString == "" || $timeout <= 0){
			return false;
		}
	 $con = curl_init((string)$url);
	 curl_setopt($con, CURLOPT_HEADER, false);
	 curl_setopt($con, CURLOPT_POSTFIELDS, $requestString);
	 curl_setopt($con, CURLOPT_POST, true);
	 curl_setopt($con, CURLOPT_RETURNTRANSFER,true);
	 curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout);	
		curl_setopt($con, CURLOPT_HTTPHEADER, array(
			'Content-Type: application/x-www-form-urlencoded')
		);
	 return curl_exec($con);
	}
	/**
	 * @desc 封装curl的调用接口,get的请求方式
	 */
	function doCurlGetRequest($url, $data = array(), $timeout = 10) {
		if($url == "" || $timeout <= 0){
			return false;
		}
		if($data != array()) {
			$url = $url . '?' . http_build_query($data);
		}
		Log::write("发送URL[".$url."]");
		$con = curl_init((string)$url);
		curl_setopt($con, CURLOPT_HEADER, false);
		curl_setopt($con, CURLOPT_RETURNTRANSFER,true);
		curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout);
		curl_setopt($con, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($con, CURLOPT_SSL_VERIFYHOST, false);
		return curl_exec($con);
	}
	/**
	 *截取字符串,中文无乱码
	*/
	function msubstr($str, $start=0, $length=0,$suffix=false,$charset="utf-8"){
	 if(mb_strlen($str,"UTF8") >= $length){
	  $next_str = "…";
	 }else{
	  $next_str = "";
	 }
	 if(function_exists("mb_substr")){
	  if($suffix)
	   // return mb_substr($str, $start, $length, $charset)."…";
	   return mb_substr($str, $start, $length, $charset).$next_str;
	  else
	   return mb_substr($str, $start, $length, $charset);
	 }elseif(function_exists('iconv_substr')) {
	  if($suffix)
	   // return iconv_substr($str,$start,$length,$charset)."…";
	   return iconv_substr($str,$start,$length,$charset).$next_str;
	  else
	   return iconv_substr($str,$start,$length,$charset);
	 }
	 $re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
	 $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";
	 $re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";
	 $re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";
	 preg_match_all($re[$charset], $str, $match);
	 $slice = join("",array_slice($match[0], $start, $length));
	  if($suffix) return $slice."…";
	 if(!$suffix) return $slice;
	 return $slice;
	}
	/**
	 *用户名*隐藏
	 *$str = "如来神掌";
	 *echo cut_str($str, 1, 0).'**'.cut_str($str, 1, -1);
	 *输出:如**掌
	*/
	function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')
	{
	 if($code == 'UTF-8')
	 {
	  $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
	  preg_match_all($pa, $string, $t_string);
	  if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen));
	  return join('', array_slice($t_string[0], $start, $sublen));
	 }
	 else
	 {
	  $start = $start*2;
	  $sublen = $sublen*2;
	  $strlen = strlen($string);
	  $tmpstr = '';
	  for($i=0; $i< $strlen; $i++)
	  {
	   if($i>=$start && $i< ($start+$sublen))
	   {
	    if(ord(substr($string, $i, 1))>129)
	    {
	     $tmpstr.= substr($string, $i, 2);
	    }
	    else
	    {
	     $tmpstr.= substr($string, $i, 1);
	    }
	   }
	   if(ord(substr($string, $i, 1))>129) $i++;
	  }
	  //if(strlen($tmpstr)< $strlen ) $tmpstr.= "...";
	  return $tmpstr;
	 }
	}
	/*
	*解决json_encode转换中文的问题,在php5.4下json_encode本身解决的这个问题
	*/
	function unescapedUnicode($match)
	{
		return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
	}
	/**
	*截取手机号
	*demo:13112345678
	*return:131****5678
	*/
	function hidtel($phone){
	 $IsWhat = preg_match('/(0[0-9]{2,3}[-]?[2-9][0-9]{6,7}[-]?[0-9]?)/i',$phone); //固定电话
	 if($IsWhat == 1){
	  return preg_replace('/(0[0-9]{2,3}[-]?[2-9])[0-9]{3,4}([0-9]{3}[-]?[0-9]?)/i','$1****$2',$phone);
	 }else{
	  return preg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$phone);
	 }
	}
	//获取IP
	function getIp()
	{
		if (isset($_SERVER)){
			if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
				$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
			} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
				$realip = $_SERVER["HTTP_CLIENT_IP"];
			} else {
				$realip = $_SERVER["REMOTE_ADDR"];
			}
		} else {
			if (getenv("HTTP_X_FORWARDED_FOR")){
				$realip = getenv("HTTP_X_FORWARDED_FOR");
			} else if (getenv("HTTP_CLIENT_IP")) {
				$realip = getenv("HTTP_CLIENT_IP");
			} else {
				$realip = getenv("REMOTE_ADDR");
			}
		}
	
		return $realip;
	}
	//把汉字转换为数字
	function getUnicodeFromOneUTF8($word)
	{
		//获取其字符的内部数组表示,所以本文件应用utf-8编码! 
		//$word = iconv("gb2312", "utf-8", $word);
		if (is_array($word)) 
			$arr = $word; 
		else 
			$arr = str_split($word); 
		//此时,$arr应类似array(228, 189, 160) 
		//定义一个空字符串存储 
		$bin_str = ''; 
		//转成数字再转成二进制字符串,最后联合起来。 
		foreach ($arr as $value) 
			$bin_str .= decbin(ord($value)); 
		//此时,$bin_str应类似111001001011110110100000
		//正则截取 
		$bin_str = preg_replace('/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/','$1$2$3', $bin_str);
		//此时, $bin_str应类似0100111101100000,如果是汉字"你"
		return bindec($bin_str); 
		//返回类似20320, 汉字"你" 
		//return dechex(bindec($bin_str)); 
		//如想返回十六进制4f60,用这句

以上这篇PHP封装curl的调用接口及常用函数详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
珊瑚虫IP库浅析
Feb 15 PHP
Ajax PHP简单入门教程代码
Apr 25 PHP
PHP mkdir()定义和用法
Jan 14 PHP
mysql总结之explain
Feb 27 PHP
PHP 提取图片img标记中的任意属性的简单实例
Dec 10 PHP
一组PHP可逆加密解密算法实例代码
Jan 21 PHP
yii2中添加验证码的实现方法
Jan 09 PHP
PHP中list方法用法示例
Dec 01 PHP
thinkPHP5.0框架开发规范简介
Mar 25 PHP
PHP实现的简单路由和类自动加载功能
Mar 13 PHP
PHP优化之批量操作MySQL实例分析
Apr 23 PHP
PHP实现简单注册登录系统
Dec 28 PHP
PHP通过curl获取接口URL的数据方法
May 31 #PHP
php获得刚插入数据的id 的几种方法总结
May 31 #PHP
PHP+MySQL使用mysql_num_rows实现模糊查询图书信息功能
May 31 #PHP
PHP实现可精确验证身份证号码的工具类示例
May 31 #PHP
JSON PHP中,Json字符串反序列化成对象/数组的方法
May 31 #PHP
php curl获取到json对象并转成数组array的方法
May 31 #PHP
php插入mysql数据返回id的方法
May 31 #PHP
You might like
php无限极分类实现的两种解决方法
2013/04/28 PHP
利用PHP命令行模式采集股票趋势信息
2016/08/09 PHP
PHP控制反转(IOC)和依赖注入(DI)
2017/03/13 PHP
jQuery Select(单选) 模拟插件 V1.3.62 改进版
2010/07/17 Javascript
jsvascript图像处理—(计算机视觉应用)图像金字塔
2013/01/15 Javascript
关于JavaScript与HTML的交互事件
2013/04/12 Javascript
浅析IE10兼容性问题(frameset的cols属性)
2014/01/03 Javascript
javascript 操作符(~、&amp;、|、^、)使用案例
2014/12/31 Javascript
基于jquery插件实现拖拽删除图片功能
2020/08/27 Javascript
angular中的http拦截器Interceptors的实现
2017/02/21 Javascript
详解Vue中添加过渡效果
2017/03/20 Javascript
详解vue mint-ui源码解析之loadmore组件
2017/10/11 Javascript
vue项目中使用axios上传图片等文件操作
2017/11/02 Javascript
JavaScript函数IIFE使用详解
2019/10/21 Javascript
JavaScript中的全局属性与方法深入解析
2020/06/14 Javascript
解决vue-photo-preview 异步图片放大失效的问题
2020/07/29 Javascript
vue2.0 watch里面的 deep和immediate用法说明
2020/10/30 Javascript
python轻松查到删除自己的微信好友
2016/01/10 Python
使用python实现链表操作
2018/01/26 Python
浅谈pytorch、cuda、python的版本对齐问题
2020/01/15 Python
使用Django xadmin 实现修改时间选择器为不可输入状态
2020/03/30 Python
Python map及filter函数使用方法解析
2020/08/06 Python
Django CBV模型源码运行流程详解
2020/08/17 Python
Lombok插件安装(IDEA)及配置jar包使用详解
2020/11/04 Python
Css3新特性应用之形状总结
2016/12/08 HTML / CSS
详解HTML5中的拖放事件(Drag 和 drop)
2016/11/14 HTML / CSS
Toppik顶丰增发纤维官网:解决头发稀疏
2017/12/30 全球购物
毕业生欢送会主持词
2014/03/31 职场文书
村党支部换届选举方案
2014/05/02 职场文书
授权委托书
2014/07/31 职场文书
中学生纪念九一八事变演讲稿
2014/09/14 职场文书
二手车转让协议书
2015/01/29 职场文书
农村婚礼司仪主持词
2015/06/29 职场文书
用基于python的appium爬取b站直播消费记录
2021/04/17 Python
python 中的jieba分词库
2021/11/23 Python
oracle设置密码复杂度及设置超时退出的功能
2022/06/28 Oracle