-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommon.php
More file actions
executable file
·113 lines (102 loc) · 3.27 KB
/
Common.php
File metadata and controls
executable file
·113 lines (102 loc) · 3.27 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* Created by PhpStorm.
* User: shawn
* Date: 2018/4/9
* Time: 16:01
*/
namespace Home\Controller;
class Common
{
/**
* post请求
* @param string $url 请求地址
* @param array $post_data 请求数据
* @param bool $is_json 是否是json流
* @return mixed
*/
protected function httpPost($url, $post_data, $is_json = true)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
if ($is_json) {
$post_data = json_encode($post_data);
$header = array(
'Content-type: application/json;charset=utf-8',
'Content-Length: ' . strlen($post_data)
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
}
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
/**
* get请求
* @param string $url 请求地址
* @return bool|string
*/
protected function httpGet($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($curl);
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$info = substr($data, $headerSize);
curl_close($curl);
return $info;
}
/**
* 根据类型返回数据
* @param int | string $status 状态码
* @param array | string $data 数据
* @param string $msg 信息
* @param string $type 类型
*/
protected function ajaxReturn($status, $data, $msg = '', $type = 'JSON')
{
switch (strtoupper($type)) {
case 'JSON' :
// 返回JSON数据格式到客户端 包含状态信息
header('Content-Type:application/json; charset=utf-8');
$data = array('code' => $status, 'content' => $data, 'msg' => $msg);
exit(json_encode($data));
case 'XML' :
// 返回xml格式数据
header('Content-Type:text/xml; charset=utf-8');
exit($this->arrayToXml($data));
case 'EVAL' :
// 返回可执行的js脚本
header('Content-Type:text/html; charset=utf-8');
exit($data);
}
}
/**
* 数组转xml格式
* @param $arr
* @return string
*/
protected function arrayToXml($arr)
{
$xml = "<xml>";
foreach ($arr as $key => $val) {
if (is_array($val)) {
$xml .= "<" . $key . ">" . $this->arrayToXml($val) . "</" . $key . ">";
} else {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
}