public static function request($url, $params = array(), $httpMethod = 'GET', $multi = false)
{
// when using bae(baidu app engine) to deploy the application,
// just comment the following line
$ch = curl_init();
// when using bae(baidu app engine) to deploy the application,
// and uncomment the following two lines
//$fetch= new BaeFetchUrl();
//$ch = $fetch->getHandle();
$curl_opts = array(CURLOPT_CONNECTTIMEOUT => 3, CURLOPT_TIMEOUT => 5, CURLOPT_USERAGENT => 'baidu-apiclient-php-2.0', 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 {
$headers = array();
if ($multi && is_array($params) && !empty($params)) {
$body = self::buildHttpMultipartBody($params);
$headers[] = 'Content-Type: multipart/form-data; boundary=' . self::$boundary;
} else {
$body = http_build_query($params, '', '&');
}
$curl_opts[CURLOPT_URL] = $url;
$curl_opts[CURLOPT_POSTFIELDS] = $body;
$curl_opts[CURLOPT_HTTPHEADER] = $headers;
}
curl_setopt_array($ch, $curl_opts);
$result = curl_exec($ch);
if ($result === false) {
self::setError(curl_errno($ch), curl_error($ch));
curl_close($ch);
return false;
} elseif (empty($result)) {
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code != 200) {
self::setError($http_code, 'http response status code: ' . $http_code);
curl_close($ch);
return false;
}
}
curl_close($ch);
return $result;
}