/**
* 发起get或者post请求
* @param string $url Url to request
* @param array $params Parameters for the request
* @param string $httpMethod Http method, 'GET' or 'POST'
* @return string|false Returns string if success, or false if failed
*/
private static function request($url, $params = array(), $httpMethod = 'GET')
{
$ch = curl_init();
$curl_opts = array(
CURLOPT_CONNECTTIMEOUT => 3,
CURLOPT_TIMEOUT => 5,
CURLOPT_USERAGENT => 'jd',
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => false,
);
if (stripos($url, 'https://') === 0) {
$curl_opts[CURLOPT_SSL_VERIFYPEER] = false;
}
if (strtoupper($httpMethod) === 'GET') {
$query = http_build_query($params, '', '&');
$delimiter = strpos($url, '?') === false ? '?' : '&';
$curl_opts[CURLOPT_URL] = $url . $delimiter . $query;
$curl_opts[CURLOPT_POST] = false;
} else {
$body = http_build_query($params, '', '&');
$curl_opts[CURLOPT_URL] = $url;
$curl_opts[CURLOPT_POSTFIELDS] = $body;
}
curl_setopt_array($ch, $curl_opts);
$result = curl_exec($ch);
curl_close($ch);
if (($result === false) || empty($result)) {
return false;
}
return $result;
}
* 发起get或者post请求
* @param string $url Url to request
* @param array $params Parameters for the request
* @param string $httpMethod Http method, 'GET' or 'POST'
* @return string|false Returns string if success, or false if failed
*/
private static function request($url, $params = array(), $httpMethod = 'GET')
{
$ch = curl_init();
$curl_opts = array(
CURLOPT_CONNECTTIMEOUT => 3,
CURLOPT_TIMEOUT => 5,
CURLOPT_USERAGENT => 'jd',
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => false,
);
if (stripos($url, 'https://') === 0) {
$curl_opts[CURLOPT_SSL_VERIFYPEER] = false;
}
if (strtoupper($httpMethod) === 'GET') {
$query = http_build_query($params, '', '&');
$delimiter = strpos($url, '?') === false ? '?' : '&';
$curl_opts[CURLOPT_URL] = $url . $delimiter . $query;
$curl_opts[CURLOPT_POST] = false;
} else {
$body = http_build_query($params, '', '&');
$curl_opts[CURLOPT_URL] = $url;
$curl_opts[CURLOPT_POSTFIELDS] = $body;
}
curl_setopt_array($ch, $curl_opts);
$result = curl_exec($ch);
curl_close($ch);
if (($result === false) || empty($result)) {
return false;
}
return $result;
}