URL.class.php
3.52 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
114
115
116
117
<?php
/* PHP SDK
* @version 2.0.0
* @author connect@qq.com
* @copyright © 2013, Tencent Corporation. All rights reserved.
*/
require_once(QC_CLASS_PATH.'ErrorCase.class.php');
/*
* @brief url封装类,将常用的url请求操作封装在一起
* */
class URL{
private $error;
public function __construct(){
$this->error = new ErrorCase();
}
/**
* combineURL
* 拼接url
* @param string $baseURL 基于的url
* @param array $keysArr 参数列表数组
* @return string 返回拼接的url
*/
public function combineURL($baseURL,$keysArr){
$combined = $baseURL."?";
$valueArr = array();
foreach($keysArr as $key => $val){
$valueArr[] = "$key=$val";
}
$keyStr = implode("&",$valueArr);
$combined .= ($keyStr);
return $combined;
}
/**
* get_contents
* 服务器通过get请求获得内容
* @param string $url 请求的url,拼接后的
* @return string 请求返回的内容
*/
public function get_contents($url){
if (ini_get("allow_url_fopen") == "1") {
//2015-3-5 wuxiao 尝试解决Maximum execution time of 30 seconds exceeded
//$response = file_get_contents($url);
$response = file_get_contents($url, false, stream_context_create(array(
'http' => array(
'method' => "GET",
'timeout' => 10,
)
)));
}else{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
//2015-3-5 wuxiao 尝试解决Maximum execution time of 30 seconds exceeded
curl_setopt ( $ci, CURLOPT_CONNECTTIMEOUT, 3 );
curl_setopt ( $ci, CURLOPT_TIMEOUT, 7 );
$response = curl_exec($ch);
curl_close($ch);
}
//-------请求为空
if(empty($response)){
$this->error->showError("50001");
}
return $response;
}
/**
* get
* get方式请求资源
* @param string $url 基于的baseUrl
* @param array $keysArr 参数列表数组
* @return string 返回的资源内容
*/
public function get($url, $keysArr){
$combined = $this->combineURL($url, $keysArr);
return $this->get_contents($combined);
}
/**
* post
* post方式请求资源
* @param string $url 基于的baseUrl
* @param array $keysArr 请求的参数列表
* @param int $flag 标志位
* @return string 返回的资源内容
*/
public function post($url, $keysArr, $flag = 0){
$ch = curl_init();
if(! $flag) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $keysArr);
curl_setopt($ch, CURLOPT_URL, $url);
//2015-3-5 wuxiao 尝试解决Maximum execution time of 30 seconds exceeded
curl_setopt ( $ci, CURLOPT_CONNECTTIMEOUT, 3 );
curl_setopt ( $ci, CURLOPT_TIMEOUT, 7 );
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
}