123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
- // require_once 'includes/WebStart.php';
- class NetUtils {
- /**
- * Set up the API root URL.
- *
- * @ignore
- *
- *
- *
- *
- */
- public $host = "https://api.vdian.com/";
- /**
- * Set timeout default.
- *
- * @ignore
- *
- *
- *
- *
- */
- public $timeout = 30;
- /**
- * Set connect timeout.
- *
- * @ignore
- */
- public $connecttimeout = 30;
- /**
- * Verify SSL Cert.
- *
- * @ignore
- */
- public $ssl_verifypeer = FALSE;
- /**
- * Respons format.
- * @ignore
- */
- public $format = 'json';
- /**
- * Decode returned json data.
- * @ignore
- */
- public $decode_json = TRUE;
- /**
- * Contains the last HTTP headers returned.
- * @ignore
- */
- public $http_info;
- /**
- * Set the useragnet.
- * @ignore
- */
- private $useragent = 'weidian OAuth2 PHP SDK v1.0';
-
- /**
- * boundary of multipart
- * @ignore
- */
- public static $boundary = '';
- public static function getInstance() {
- return new NetUtils ();
- }
- /**
- * HTTP GET 请求
- *
- * @param unknown $url
- * @return HttpResponse
- */
- public function get($url) {
- if (strrpos ( $url, 'http://' ) !== 0 && strrpos ( $url, 'https://' ) !== 0) {
- $url = "{$this->host}{$url}.{$this->format}";
- }
- return $this->http ( $url, 'GET' );
- }
- /**
- * POST request
- *
- * @param string $url
- * @param array $parameters
- * @return HttpResponse
- */
- public function post($url, array $parameters) {
- if (strrpos ( $url, 'http://' ) !== 0 && strrpos ( $url, 'https://' ) !== 0) {
- $url = "{$this->host}{$url}.{$this->format}";
- }
- $body = null;
- if (DEBUG) {
- Logger::debug ( "URL->" . $url );
- if (is_array ( $parameters )) {
- while ( list ( $key, $val ) = each ( $parameters ) ) {
- Logger::debug ( $key . "=" . $val );
- }
- }
- }
- $body = http_build_query ( $parameters );
- return $this->http ( $url, 'POST', $body );
- }
- /**
- * 发送HTTP请求
- *
- * @param string $url
- * @param string $method
- * @param unknown $parameters
- * @param bool $multi
- * @return HttpResponse
- */
- public function request($url, $method, $parameters, $multi = false) {
- if (strrpos ( $url, 'http://' ) !== 0 && strrpos ( $url, 'https://' ) !== 0) {
- // 支持相对路径
- $url = "{$this->host}{$url}.{$this->format}";
- }
-
- switch ($method) {
- case 'GET' :
- $url = $url . '?' . http_build_query ( $parameters );
- return $this->http ( $url, 'GET' );
- default :
- $headers = array ();
- if (! $multi && (is_array ( $parameters ) || is_object ( $parameters ))) {
- $body = http_build_query ( $parameters );
- } else {
- $body = self::build_http_query_multi ( $parameters );
- $headers [] = "Content-Type: multipart/form-data; boundary=" . self::$boundary;
- }
- return $this->http ( $url, $method, $body, $headers );
- }
- }
- /**
- * 上传文件
- *
- * @param unknown $url
- * @param unknown $parameters
- * @return Ambigous <string, mixed>
- */
- public function uploadFile($url, $parameters) {
- $headers = array ();
- $body = self::build_http_query_multi ( $parameters );
- $headers [] = "Content-Type: multipart/form-data; boundary=" . self::$boundary;
- return self::http ( $url, 'POST', $body, $headers );
- }
- /**
- * Make an HTTP request
- *
- * @return string API results
- * @ignore
- *
- *
- *
- *
- */
- private function http($url, $method, $postfields = NULL, $headers = array()) {
- $response = new HttpResponse ( $url, $headers, $method, $postfields );
- $ci = curl_init ();
- /* Curl settings */
- curl_setopt ( $ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
- curl_setopt ( $ci, CURLOPT_USERAGENT, $this->useragent );
- curl_setopt ( $ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout );
- curl_setopt ( $ci, CURLOPT_TIMEOUT, $this->timeout );
- curl_setopt ( $ci, CURLOPT_RETURNTRANSFER, TRUE );
- curl_setopt ( $ci, CURLOPT_ENCODING, "UTF-8" );
- curl_setopt ( $ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer );
- curl_setopt ( $ci, CURLOPT_SSL_VERIFYHOST, "2" );
- curl_setopt ( $ci, CURLOPT_HEADERFUNCTION, array (
- $this,
- 'getHeader'
- ) );
- curl_setopt ( $ci, CURLOPT_HEADER, FALSE );
-
- switch ($method) {
- case 'POST' :
- curl_setopt ( $ci, CURLOPT_POST, TRUE );
- if (! empty ( $postfields )) {
- curl_setopt ( $ci, CURLOPT_POSTFIELDS, $postfields );
- $this->postdata = $postfields;
- }
- break;
- case 'DELETE' :
- curl_setopt ( $ci, CURLOPT_CUSTOMREQUEST, 'DELETE' );
- if (! empty ( $postfields )) {
- $url = "{$url}?{$postfields}";
- }
- }
- $headers [] = "API-RemoteIP: " . $_SERVER ['REMOTE_ADDR'];
- curl_setopt ( $ci, CURLOPT_URL, $url );
- curl_setopt ( $ci, CURLOPT_HTTPHEADER, $headers );
- curl_setopt ( $ci, CURLINFO_HEADER_OUT, TRUE );
-
- $response->data = curl_exec ( $ci );
- $response->http_code = curl_getinfo ( $ci, CURLINFO_HTTP_CODE );
- $response->http_info = array_merge ( $response->http_info, curl_getinfo ( $ci ) );
- $response->url = $url;
- if (DEBUG) {
- Logger::debug ( "headers:" );
- print_r ( $headers );
- Logger::debug ( "requestInfo:" );
- print_r ( curl_getinfo ( $ci ) );
- Logger::debug ( "body:" );
- print_r ( $postfields );
- }
- Logger::debug ( "返回:" . $response->data );
- curl_close ( $ci );
- return $response;
- }
-
- /**
- *
- * @ignore
- *
- *
- *
- */
- private function build_http_query_multi($params) {
- if (! $params)
- return '';
-
- uksort ( $params, 'strcmp' );
-
- $pairs = array ();
-
- self::$boundary = $boundary = uniqid ( '===============' );
- $MPboundary = '--' . $boundary;
- $endMPboundary = $MPboundary . '--';
- $multipartbody = '';
-
- foreach ( $params as $parameter => $value ) {
-
- if (in_array ( $parameter, array (
- 'media'
- ) ) && $value {0} == '@') {
- $url = ltrim ( $value, '@' );
- $content = file_get_contents ( $url );
- $array = explode ( '?', basename ( $url ) );
- $filename = $array [0];
-
- $multipartbody .= $MPboundary . "\r\n";
- $multipartbody .= 'Content-Disposition: form-data; name="' . $parameter . '"; filename="' . $filename . '"' . "\r\n";
- $multipartbody .= "Content-Type: image/unknown\r\n\r\n";
- $multipartbody .= $content . "\r\n";
- } else {
- $multipartbody .= $MPboundary . "\r\n";
- $multipartbody .= 'content-disposition: form-data; name="' . $parameter . "\"\r\n\r\n";
- $multipartbody .= $value . "\r\n";
- }
- }
-
- $multipartbody .= $endMPboundary;
- return $multipartbody;
- }
-
- /**
- * Get the header info to store.
- *
- * @return int
- * @ignore
- *
- *
- *
- *
- *
- *
- */
- function getHeader($ch, $header) {
- $i = strpos ( $header, ':' );
- if (! empty ( $i )) {
- $key = str_replace ( '-', '_', strtolower ( substr ( $header, 0, $i ) ) );
- $value = trim ( substr ( $header, $i + 2 ) );
- $this->http_header [$key] = $value;
- }
- return strlen ( $header );
- }
- }
- ;
- ?>
|