httpClient.php
1.37 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
<?php
header ( 'Content-type:text/html;charset=utf-8' );
/**
* 后台交易 HttpClient通信
* @param unknown_type $params
* @param unknown_type $url
* @return mixed
*/
function sendHttpRequest($params, $url) {
$opts = getRequestParamString ( $params );
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false);//不验证证书
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false);//不验证HOST
curl_setopt ( $ch, CURLOPT_SSLVERSION, 3);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
'Content-type:application/x-www-form-urlencoded;charset=UTF-8'
) );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $opts );
/**
* 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
*/
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
// 运行cURL,请求网页
$html = curl_exec ( $ch );
// close cURL resource, and free up system resources
curl_close ( $ch );
return $html;
}
/**
* 组装报文
*
* @param unknown_type $params
* @return string
*/
function getRequestParamString($params) {
$params_str = '';
foreach ( $params as $key => $value ) {
$params_str .= ($key . '=' . (!isset ( $value ) ? '' : urlencode( $value )) . '&');
}
return substr ( $params_str, 0, strlen ( $params_str ) - 1 );
}