123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- /**
- * http请求函数
- *
- *
- *
- */
- function http_request($url, $params = array(), $method = 'GET', $multi = false, $extheaders = array())
- {
- if (!function_exists('curl_init')) {
- return false;
- }
- $method = strtoupper($method);
- $ci = curl_init();
- curl_setopt($ci, CURLOPT_USERAGENT, 'PHP-SDK OAuth2.0');
- curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
- curl_setopt($ci, CURLOPT_TIMEOUT, 30);
- curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ci, CURLOPT_HEADER, false);
- $headers = (array) $extheaders;
- switch ($method)
- {
- case 'POST':
- curl_setopt($ci, CURLOPT_POST, TRUE);
- if (!empty($params))
- {
- if ($multi)
- {
- foreach ($multi as $key => $file) {
- $params[$key] = '@' . $file;
- }
- curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
- $headers[] = 'Expect: ';
- }
- else {
- curl_setopt($ci, CURLOPT_POSTFIELDS, http_build_query($params));
- }
- }
- break;
- case 'DELETE':
- case 'GET':
- if($method == 'DELETE') {
- curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
- }
- if (!empty($params)) {
- $url = $url . (strpos($url, '?') ? '&' : '?') . (is_array($params) ? http_build_query($params) : $params);
- }
- break;
- }
- curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
- curl_setopt($ci, CURLOPT_URL, $url);
- if ($headers) {
- curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
- }
- $response = curl_exec($ci);
- if($response === false) {
- $err = curl_error($ci);
- }
- curl_close($ci);
- return $response;
- }
- function http_post_data($url, $body, $headers = array())
- {
- if (!function_exists('curl_init')) {
- return false;
- }
- $ci = curl_init();
- curl_setopt($ci, CURLOPT_USERAGENT, 'PHP-SDK OAuth2.0');
- curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 3);
- curl_setopt($ci, CURLOPT_TIMEOUT, 15);
- curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ci, CURLOPT_HEADER, false);
- curl_setopt($ci, CURLOPT_POSTFIELDS, $body);
- curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
- curl_setopt($ci, CURLOPT_URL, $url);
- if (!empty($headers)) {
- curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
- }
- $response = curl_exec($ci);
- if($response == false) {
- $err = curl_error($ci);
- Log::record("http_post_data err={$err}",Log::ERR);
- }
- curl_close($ci);
- return $response;
- }
- function https_request($url, $params = array(), $method = 'GET', $multi = false, $extheaders = array())
- {
- if (!function_exists('curl_init')) {
- return false;
- }
- $method = strtoupper($method);
- $ci = curl_init();
- curl_setopt($ci, CURLOPT_USERAGENT, 'PHP-SDK OAuth2.0');
- curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 3);
- curl_setopt($ci, CURLOPT_TIMEOUT, 3);
- curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, 2);
- curl_setopt($ci, CURLOPT_HEADER, false);
- $headers = (array) $extheaders;
- switch ($method)
- {
- case 'POST':
- curl_setopt($ci, CURLOPT_POST, TRUE);
- if (!empty($params))
- {
- if ($multi)
- {
- foreach ($multi as $key => $file) {
- $params[$key] = '@' . $file;
- }
- curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
- $headers[] = 'Expect: ';
- }
- else {
- curl_setopt($ci, CURLOPT_POSTFIELDS, http_build_query($params));
- }
- }
- break;
- case 'DELETE':
- case 'GET':
- if($method == 'DELETE') {
- curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
- }
- if (!empty($params)) {
- $url = $url . (strpos($url, '?') ? '&' : '?') . (is_array($params) ? http_build_query($params) : $params);
- }
- break;
- }
- curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
- curl_setopt($ci, CURLOPT_URL, $url);
- if ($headers) {
- curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
- }
- $response = curl_exec($ci);
- curl_close($ci);
- return $response;
- }
|