1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- /**
- * http请求函数
- *
- *
- *
- */
- defined('InShopNC') or exit('Access Invalid!');
- function http_request($url, $params = array(), $method = 'GET', $multi = false, $extheaders = array())
- {
- if (!function_exists('curl_init')) {
- return NULL;
- }
- $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, 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':
- $method == 'DELETE' && curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
- if (!empty($params)) {
- $url = $url . (strpos($url, '?') ? '&' : '?')
- . (is_array($params) ? http_build_query($params) : $params);
- }
- echo $url;
- 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;
- }
- function http_post_data($url, $body, $headers = array())
- {
- if (!function_exists('curl_init')) {
- return NULL;
- }
- $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, 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 ($headers) {
- curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
- }
- $response = curl_exec($ci);
- curl_close($ci);
- return $response;
- }
|