目录

CURL

PHP对CURL函数的封装,支持GET/POST请求

代码如下

  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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232

		/**
		 * curl 函数
		 * @param string $url 请求的地址
		 * @param string $type POST/GET/post/get
		 * @param array $data 要传输的数据
		 * @param string $err_msg 可选的错误信息(引用传递)
		 * @param int $timeout 超时时间
		 * @param array 证书信息
		 * @author 勾国印
		 */
		function GoCurl($url, $type, $data = false, &$err_msg = null, $timeout = 20, $cert_info = array())
		{
		    $type = strtoupper($type);
		    if ($type == 'GET' && is_array($data)) {
		        $data = http_build_query($data);
		    }
		
		    $option = array();
		
		    if ( $type == 'POST' ) {
		        $option[CURLOPT_POST] = 1;
		    }
		    if ($data) {
		        if ($type == 'POST') {
		            $option[CURLOPT_POSTFIELDS] = $data;
		        } elseif ($type == 'GET') {
		            $url = strpos($url, '?') !== false ? $url.'&'.$data :  $url.'?'.$data;
		        }
		    }
		
		    $option[CURLOPT_URL]            = $url;
		    $option[CURLOPT_FOLLOWLOCATION] = TRUE;
		    $option[CURLOPT_MAXREDIRS]      = 4;
		    $option[CURLOPT_RETURNTRANSFER] = TRUE;
		    $option[CURLOPT_TIMEOUT]        = $timeout;
		
		    //设置证书信息
		    if(!empty($cert_info) && !empty($cert_info['cert_file'])) {
		        $option[CURLOPT_SSLCERT]       = $cert_info['cert_file'];
		        $option[CURLOPT_SSLCERTPASSWD] = $cert_info['cert_pass'];
		        $option[CURLOPT_SSLCERTTYPE]   = $cert_info['cert_type'];
		    }
		
		    //设置CA
		    if(!empty($cert_info['ca_file'])) {
		        // 对认证证书来源的检查,0表示阻止对证书的合法性的检查。1需要设置CURLOPT_CAINFO
		        $option[CURLOPT_SSL_VERIFYPEER] = 1;
		        $option[CURLOPT_CAINFO] = $cert_info['ca_file'];
		    } else {
		        // 对认证证书来源的检查,0表示阻止对证书的合法性的检查。1需要设置CURLOPT_CAINFO
		        $option[CURLOPT_SSL_VERIFYPEER] = 0;
		    }
		
		    $ch = curl_init();
		    curl_setopt_array($ch, $option);
		    $response = curl_exec($ch);
		    $curl_no  = curl_errno($ch);
		    $curl_err = curl_error($ch);
		    curl_close($ch);
		
		    // error_log
		    if($curl_no > 0) {
		        if($err_msg !== null) {
		            $err_msg = '('.$curl_no.')'.$curl_err;
		        }
		    }
		    return $response;
		}

 使用方法如下: 

		$url   = '请求地址';
		$data = array(
		            'phoneNum' => '18614064456',
		        );
		$json = GoCurl($url, $data, 'POST', $error_msg);
		
		$array = json_decode($json, true);
		
		print_r($array);

## curl ##
	   $ch = curl_init();
	
	   //设置超时
	
	   curl_setopt($ch, CURLOPT_TIMEOUT,30);
	
	   //这里设置代理,如果有的话
	
	   //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
	
	   //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
	
	   curl_setopt($ch,CURLOPT_URL,$url);
	
	   curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
	
	   curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
	
	   //设置header
	
	   curl_setopt($ch, CURLOPT_HEADER, FALSE);
	
	   //要求结果为字符串且输出到屏幕上
	
	   curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	
	   //post提交方式
	
	   $all_param = 'content='.$content;
	
	   curl_setopt($ch, CURLOPT_POST, TRUE);
	
	   //开始请求
	
	   curl_setopt($ch, CURLOPT_POSTFIELDS, $all_param);
	
	   //运行curl
	
	   $data = curl_exec($ch);
	
	   curl_close($ch);

## post方式

	$data_string :要传输的数据
	
	//初始化
	
	 $ch = curl_init();
	
	 curl_setopt($ch, CURLOPT_URL, '访问的接口地址');
	
	 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
	
	 curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
	
	 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
	
	 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_string)));
	
	//执行并获取接口传的内容
	
	 $ret = curl_exec($ch);
	
	function http_post_json($url, $jsonStr)
	
	{
	
	  $ch = curl_init();
	
	  curl_setopt($ch, CURLOPT_POST, 1);
	
	  curl_setopt($ch, CURLOPT_URL, $url);
	
	  curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
	
	  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	
	  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	
	      'Content-Type: application/json; charset=utf-8',
	
	      'Content-Length: ' . strlen($jsonStr)
	
	    )
	
	  );
	
	  $response = curl_exec($ch);
	
	  $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	
	  return array($httpCode, $response);
	
	}
	
	$url = "http://blog.snsgou.com";
	
	$jsonStr = json_encode(array('a' => 1, 'b' => 2));
	
	list($returnCode, $returnContent) = http_post_json($url, $jsonStr);
	
	function java_id($url,$content){
	
	$ch = curl_init();
	
	//设置超时
	
	curl_setopt($ch, CURLOPT_TIMEOUT,30);
	
	//这里设置代理,如果有的话
	
	//curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
	
	//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
	
	curl_setopt($ch,CURLOPT_URL,$url);
	
	curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
	
	curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
	
	//设置header
	
	curl_setopt($ch, CURLOPT_HEADER, FALSE);
	
	//要求结果为字符串且输出到屏幕上
	
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	
	//post提交方式
	
	$all_param = $content;
	
	curl_setopt($ch, CURLOPT_POST, TRUE);
	
	//开始请求
	
	curl_setopt($ch, CURLOPT_POSTFIELDS, $all_param);
	
	//运行curl
	
	$data = curl_exec($ch);
	
	return $data;
	
	curl_close($ch);
	
	}