curl_init 接口

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
//
function is_not_empty_string($str = '')
{
return is_string($str) && $str != '';
}

function is_positive_integer($num = 0)
{
if (is_numeric($num)) {
return $num;
}

}

function is_positive_int($num = 0)
{
return is_numeric($num) && $num != '';

}

/*功能:统计二维数组中数组的个数
*@param array 要统计的数组
*@return int $c 返回二维数组中数组的个数*/

function array_count($arr)
{
$c = 0;
foreach ($arr as $v) {
if (is_array($v)) {
$c++;
}
}
return $c;
}

function aaa($a)
{
echo "<pre>";
print_r($a);
echo "</pre>";
}

/*函数名称:get_code()
*作用:取得随机字符串
* 参数:
1、(int)$length = 32 #随机字符长度
2、(int)$mode = 0 #随机字符类型,
0为大小写英文和数字,1为数字,2为小写字母,3为大写字母,
4为大小写字母,5为大写字母和数字,6为小写字母和数字
*返回:取得的字符串
*/
function get_code($length = 32, $mode = 0) //获取随机验证码函数

{
switch ($mode) {
case '1':
$str = '123456789';
break;
case '2':
$str = 'abcdefghijklmnopqrstuvwxyz';
break;
case '3':
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case '4':
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
break;
case '5':
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
break;
case '6':
$str = 'abcdefghijklmnopqrstuvwxyz1234567890';
break;
default:
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
break;
}
$checkstr = '';
$len = strlen($str) - 1;
for ($i = 0; $i < $length; $i++) {
//$num=rand(0,$len);//产生一个0到$len之间的随机数
$num = mt_rand(0, $len); //产生一个0到$len之间的随机数
$checkstr .= $str[$num];

}
return $checkstr;
}

/**
* 准备工作完毕 开始计算年龄函数
* @param $birthday 出生时间 uninx时间戳
* @param $time 当前时间
**/
function getAge($birthday){

//格式化出生时间年月日
$byear=date('Y',$birthday);
$bmonth=date('m',$birthday);
$bday=date('d',$birthday);

//格式化当前时间年月日
$tyear=date('Y');
$tmonth=date('m');
$tday=date('d');

//开始计算年龄
$age=$tyear-$byear;
if($bmonth>$tmonth || $bmonth==$tmonth && $bday>$tday){
$age--;
}
return $age;
}

 

/**
* [build_order_no 生成唯一订单号]
*/
function StrOrderOne(){
/* 选择一个随机的方案 */
mt_srand((double) microtime() * 1000000);
return date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
}


/*短信验证函数*/
function shortmessa($code,$phone){

/*
***聚合数据(JUHE.CN)短信API服务接口PHP请求示例源码
***DATE:2015-05-25
*/
header('content-type:text/html;charset=utf-8');

$sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
$smsConf = array(
'key' => '582d1bdc30989ab6920035f0d2a7848a', //您申请的APPKEY
'mobile' => $phone, //接受短信的用户手机号码
'tpl_id' => '62736', //您申请的短信模板ID,根据实际情况修改
'tpl_value' => urlencode('#code#=' . $code . '&#company#=聚合数据'), //您设置的模板变量,根据实际情况修改
);

$content = juhecurl($sendUrl,$smsConf,1); //请求发送短信

if($content){
$result = json_decode($content,true);
$error_code = $result['error_code'];
if($error_code == 0){
//状态为0,说明短信发送成功
echo "短信发送成功,短信ID:".$result['result']['sid'];
}else{
//状态非0,说明失败
$msg = $result['reason'];
echo "短信发送失败(".$error_code."):".$msg;
}
}else{
//返回内容异常,以下可根据业务逻辑自行修改
echo "请求发送短信失败";
}

}

/**
* 请求接口返回内容
* @param string $url [请求的URL地址]
* @param string $params [请求的参数]
* @param int $ipost [是否采用POST形式]
* @return string
*/
function juhecurl($url,$params=false,$ispost=0){
$httpInfo = array();
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
if( $ispost )
{
curl_setopt( $ch , CURLOPT_POST , true );
curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
curl_setopt( $ch , CURLOPT_URL , $url );
}
else
{
if($params){
curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
}else{
curl_setopt( $ch , CURLOPT_URL , $url);
}
}
$response = curl_exec( $ch );
if ($response === FALSE) {
//echo "cURL Error: " . curl_error($ch);
return false;
}
$httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
curl_close( $ch );
return $response;
}


/**
* 通过URL获取页面信息
* @param string $url 地址
* @return string 返回页面信息
*/
function get_url($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url); //设置访问的url地址
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不输出内容
$result = curl_exec($ch);
curl_close ($ch);
return $result;
}

上一篇:菜鸟电子面单 类 php


下一篇:php curl post请求中携带header参数