common.php
2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
* 发起http post请求(REST API), 并获取REST请求的结果
*
* @param string $url
* @param string $param
*
* @return - http response body if succeeds, else false.
*/
function request_post( $url = '', $param = '' ) {
if ( empty( $url ) || empty( $param ) ) {
return false;
}
$postUrl = $url;
$curlPost = $param;
// 初始化curl
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $postUrl );
curl_setopt( $curl, CURLOPT_HEADER, 0 );
// 要求结果为字符串且输出到屏幕上
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
// post提交方式
curl_setopt( $curl, CURLOPT_POST, 1 );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $curlPost );
// 运行curl
$data = curl_exec( $curl );
curl_close( $curl );
return $data;
}
function getBuAccessToken() {
$key = config( 'baidu.client_id' );
$secret = config( 'baidu.client_secret' );
$post_data['grant_type'] = 'client_credentials';
$post_data['client_id'] = $key;
$post_data['client_secret'] = $secret;
if ( session( 'access_token_' . $key ) && session( 'expire_time_' . $key ) > time() ) {
return session( 'access_token_' . $key );
} else {
$url = 'https://aip.baidubce.com/oauth/2.0/token';
$o = "";
foreach ( $post_data as $k => $v ) {
$o .= "$k=" . urlencode( $v ) . "&";
}
$post_data = substr( $o, 0, - 1 );
$result = request_post( $url, $post_data );
$result = json_decode( $result, true );
session( 'access_token_' . $key, $result['access_token'] );
Session( 'expire_time_' . $key, time() + 2592000 );
return $result['access_token'];
}
}
//处理富文本图片,通用
function set_img_style( $activity ) {
$activity = htmlspecialchars_decode( $activity );//处理富文本标签
$activity = preg_replace( "/width: [0-9]*px;/", "max-width:100%;", $activity );
$activity = preg_replace( "/width=\"[0-9]*\"/", "", $activity );
$activity = str_replace( "<img", "<img style='max-width:100%;height:auto;'", $activity );
return str_replace( "style=\"", "style=\"width:100%;", $activity );
}
/**
* 获取用户唯一订单号
*
* @param int $uid 用户ID
*
* @return string
*/
function getOrderNo( $uid ) {
list( $msec, $sec ) = explode( ' ', microtime() );
$msectime = (float) sprintf( '%.0f', ( floatval( $msec ) + floatval( $sec ) ) * 1000 );
$res = $msectime . sprintf( '%03d', (int) $uid % 1000 ) . str_pad( mt_rand( 1, 999999 ), 6, '0', STR_PAD_LEFT );
return substr( $res, 0, 7 ) . date( 'YmdHis' );
}