<?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的调用接口及常用函数的更多相关文章

  1. curl模拟调用接口

    curl模拟调用接口 1. get请求 curl -i -X GET http://url/bind/agentOnWork/v2?Sig=******* 2. post请求(带头信息以及参数) cu ...

  2. php 调用接口

    1.一般常用方法 file_get_contents(): 今天调用了一个反人类的接口,接口是get方式传输,里面有一个参数是当前时间,没错,不是时间戳,是当前格式化的时间:2017-8-9 11:1 ...

  3. SQLite常用函数及语句

    SQLite3.0使用的是C的函数接口,常用函数如下: sqlite3_open() //打开数据库 sqlite3_close() //关闭数据库 sqlite3_exec() //执行sql语句, ...

  4. PHP其它常用函数;<<<面向对象(OPP)的三大特性:封装、继承、加态:>>> <----面试题 ;构造方法、析构方法,魔术方法、set、get方法;静态;抽象类;接口

    PHP其它常用函数:     赋值:$r->name = "元素";      取值: echo $r->name;  count()   计算数组中的元素数目或对象中 ...

  5. Thinkphp3.2.3框架下封装公共的函数,例如封装CURL函数来获取接口数据

    当我们需要在控制层调用相同的封装函数时,写多次相同的函数,显得代码十分的拉杂,不精简: TP框架有一个很好的机制,可以再Common定义一个function.php函数,当我们在控制层调用的时候直接调 ...

  6. 用ladon框架封装Python为Webservice接口以及调用接口的方法

    一.用ladon框架封装Python为Webservice接口 功能实现的同时,希望将接口开放给别人,而封装python接口的一个再简单不过的框架Ladon,而且提供不同的协议,包括SOAP和Json ...

  7. 一个完整的SAP RFC调用接口封装

    因为经常需要访问sap操作数据,就封装了一个类方便调用,运行条件需要安装sap客户端,在sap客户端安装之后会带有一个com接口,本接口就通过这个com访问sap,因为com的后期绑定问题故使用了vb ...

  8. java如何调用接口方式二

    java如何调用接口 在实际开发过程中,我们经常需要调用对方提供的接口或测试自己写的接口是否合适,所以,问题来了,java如何调用接口?很多项目都会封装规定好本身项目的接口规范,所以大多数需要去调用对 ...

  9. java通过java.net.URL发送http请求调用接口

    一般在*.html,*.jsp页面中我们通过使用ajax调用接口,这个是我们通常用的.对于这些接口,大都是本公司写的接口供自己调用,所以直接用ajax就可以.但是,如果是多家公司共同开发一个东西,一个 ...

随机推荐

  1. WEB服务器、应用程序服务器、HTTP服务器的区别

    WEB服务器.应用程序服务器.HTTP服务器的区别 Web服务器: 基本功能就是提供Web信息浏览服务.它只需支持HTTP协议.HTML文档格式及URL.与客户端的网络浏览器配合.因为Web服务器主要 ...

  2. 【读书笔记】iOS-iOS6 Passbook应用开发

    Passbook 是iOS6的新功能,只能在iPhone和iPod touch设备中使用,它可以帮助管理商家发放的电子会员卡,积分卡,优惠券等. 一,Passbook 与 Pass. Passbook ...

  3. 过滤器(Filter)与拦截器(Interceptor)的区别

    1 .拦截器是基于java的反射机制的,而过滤器是基于函数回调. 2 .拦截器不依赖与servlet容器,过滤器依赖与servlet容器. 3 .拦截器只能对action请求起作用,而过滤器则可以对几 ...

  4. js 回调函数理解与应用

    定义:在JavaScript中,回调函数具体的定义为:函数A作为参数(函数引用)传递到另一个函数B中,并且这个函数B执行函数A.我们就说函数A叫做回调函数.如果没有名称(函数表达式),就叫做匿名回调函 ...

  5. C#与java中的AES加解密互解算法

    一.C#版AES加解密算法 public class AESCode { public string Key { get; set; } public string Encrypt(string va ...

  6. Maven学习(五)使用Maven构建多模块项目

    使用Maven构建多模块项目 一般的web项目构成: 建立解决方案目录parent 首先使用命令进入到我们需要建立maven项目的目录: mvn archetype:generate -DgroupI ...

  7. php 实现简单加入购物车(1)

          这个购物车相对来说比较简单,用于短暂存储,并没有存储到数据库,购物车对于爱网购的人来说简直是熟悉的不能再熟悉了,在写购物车之前,我们首先要构思一下,我们需要先从数据库中调出一张表格,这里我 ...

  8. JSP内置对象——page对象

    观察可发现,这里面的方法,就是Object这个类下的一些方法,下面进行一个简单的演示,比如“toString()”方法: 运行结果: 这时候看到了一个“org.apache.jsp.page_jsp@ ...

  9. RecyclerView打造通用的万能Adapter

    既然想做到通用那么现在摆在面前的就三个问题:数据怎么办?布局怎么办? 绑定怎么办?.数据决定采用泛型,布局打算直接构造传递,绑定显示效果肯定就只能回传. 1 基本改造 数据决定采用泛型,布局打算直接构 ...

  10. Django from表单及ajax提交文件

    参考: https://blog.csdn.net/baobao267/article/details/83038323