PHP   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP封装curl的调用接口及常用函数详解大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

今天小编就为大家分享一篇PHP封装curl的调用接口及常用函数详解,具有很好的参价值,希望对大家有所帮助。一起跟随小编过来看看吧

如下所示:

php;toolbar:false"><>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,curlopt_timeout,(int)$timeout);="" curl_setopt($con,curlopt_ssl_verifypeer,false);="" curl_setopt($con,curlopt_ssl_verifyhost,false);="" return curl_exec($con);="" }="" *="">调用接口,post的请求方式
*/
function doCurlPostsslrequest($url,$pemfile,$timeout = 5) {
if($url == "" || $requestString == "" || $timeout <= 0){ return false;="" }="" echo $pemfile;="" $con =" curl_init((string)$url);" curl_setopt($con,1);="" curl_setopt($con,2);="" curl_setopt($ch,curlopt_sslcert,$pemfile);="" curl_setopt($ch,curlopt_sslcerttype,'pem');="" curl_setopt($con,curlopt_cainfo,$pemfile);="" return curl_exec($con);="" }="" *="">调用接口,post Json数据的请求方式
*/
function doCurlPostJsonReq($url,$timeout = 25){
if($url == "" || $requestString == "" || $timeout <= 0){ return false;="" }="" $con =" curl_init((string)$url);" curl_setopt($con,(int)$timeout);="" curl_setopt($con,curlopt_httpheader,array(="" 'content-type: application/json','content-length: ' . strlen($data_string))="" );="" return curl_exec($con);="" }="" *="">调用接口,post form数据的请求方式
*/
function doCurlPostFormReq($url,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,true);="" curl_setopt($con,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,$charset).$next_str;
 else
return mb_substr($str,$charset);
}elseif(function_exists('iconv_substr')) {
 if($suffiX)
// return iconv_substr($str,$charset)."…";
return iconv_substr($str,$charset).$next_str;
 else
return iconv_substr($str,$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],$length));
 if($suffiX) return $slice."…";
if(!$suffiX) return $slice;
return $slice;
}
/**
*用户名*隐藏
*$str = "如来神掌";
*echo cut_str($str,1,0).'**'.cut_str($str,-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],$sublen));
 return join('',$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,2);
 }
 else
 {
$tmpstr.= substr($String,1);
 }
}
if(ord(substr($String,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',$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封装curl的调用接口及常用函数详解全部内容,希望文章能够帮你解决PHP封装curl的调用接口及常用函数详解所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。