0

0

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

php中文网

php中文网

发布时间:2016-07-28 08:28:54

|

1088人浏览过

|

来源于php中文网

原创

php
	/**
	 * @desc 封装curl的调用接口,post的请求方式
	 */function doCurlPostRequest($url, $requestString, $timeout =5){if($url ==""|| $requestString ==""|| $timeout <=0){returnfalse;}
		 $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){returnfalse;}		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){returnfalse;}
	    $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){returnfalse;}
	    $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){returnfalse;}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;elsereturn 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;elsereturn 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);}}//获取IPfunction getIp(){if(isset($_SERVER)){if(isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
				$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];}elseif(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");}elseif(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,用这句  

}

网站访问地址:http://www.5k5c.com/blog/179

IJPay聚合支付SDK
IJPay聚合支付SDK

IJPay聚合支付SDK让支付触手可及,封装了微信支付、支付宝支付、银联支付常用的支付方式以及各种常用的接口。不依赖任何第三方mvc框架,仅仅作为工具使用简单快速完成支付模块的开发,可轻松嵌入到任何系统里。

下载

以上就介绍了 PHP封装curl的调用接口及常用函数,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Word 字间距调整方法汇总
Word 字间距调整方法汇总

本专题整合了Word字间距调整方法,阅读下面的文章了解更详细操作。

2

2025.12.24

任务管理器教程
任务管理器教程

本专题整合了任务管理器相关教程,阅读下面的文章了解更多详细操作。

2

2025.12.24

AppleID格式
AppleID格式

本专题整合了AppleID相关内容,阅读专题下面的文章了解更多详细教程。

0

2025.12.24

csgo视频观看入口合集
csgo视频观看入口合集

本专题整合了csgo观看入口合集,阅读下面的文章了知道更多入口地址。

29

2025.12.24

yandex外贸入口合集
yandex外贸入口合集

本专题汇总了yandex外贸入口地址,阅读下面的文章了解更多内容。

58

2025.12.24

添加脚注通用方法
添加脚注通用方法

本专题整合了添加脚注方法合集,阅读专题下面的文章了解更多内容。

1

2025.12.24

重启电脑教程汇总
重启电脑教程汇总

本专题整合了重启电脑操作教程,阅读下面的文章了解更多详细教程。

3

2025.12.24

纸张尺寸汇总
纸张尺寸汇总

本专题整合了纸张尺寸相关内容,阅读专题下面的文章了解更多内容。

5

2025.12.24

Java Spring Boot 微服务实战
Java Spring Boot 微服务实战

本专题深入讲解 Java Spring Boot 在微服务架构中的应用,内容涵盖服务注册与发现、REST API开发、配置中心、负载均衡、熔断与限流、日志与监控。通过实际项目案例(如电商订单系统),帮助开发者掌握 从单体应用迁移到高可用微服务系统的完整流程与实战能力。

1

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Excel 教程
Excel 教程

共162课时 | 9.3万人学习

Pandas 教程
Pandas 教程

共15课时 | 0.8万人学习

C# 教程
C# 教程

共94课时 | 5.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号