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; } /** * Prints to the error log if you aren't in command line mode. * * @param String log message */ public static function errorLog($msg) { // disable error log if we are running in a CLI environment if (php_sapi_name() != 'cli') { error_log($msg); } // Set the debug mode if you want to see the errors on the page if (self::$isDebug) { echo 'error_log: '.$msg."\n"; } } /** * Generate the signature for passed parameters. * * @param array $params Array of parameters to be signatured * @param string $secret Secret key for signature * @param string $namespace The parameter which will be excluded when calculate the signature * @return string Signature of the parameters */ public static function generateSign($params, $secret, $namespace = 'sign') { $str = ''; ksort($params); foreach ($params as $k => $v) { if ($k != $namespace) { $str .= "$k=$v"; } } $str .= $secret; return md5($str); } /** * Get the url of current page. * * @return string */ public static function getCurrentUrl() { $protocol = 'http://'; if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) { $protocol = strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) . '://'; } elseif (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') { $protocol = 'https://'; } if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) { $host = $_SERVER['HTTP_X_FORWARDED_HOST']; } else { $host = $_SERVER['HTTP_HOST']; } $currentUrl = $protocol . $host . $_SERVER['REQUEST_URI']; $parts = parse_url($currentUrl); $query = ''; if (!empty($parts['query'])) { // drop known oauth params $params = explode('&', $parts['query']); $retained_params = array(); foreach ($params as $param) { if (self::shouldRetainParam($param)) { $retained_params[] = $param; } } if (!empty($retained_params)) { $query = '?' . implode($retained_params, '&'); } } // use port if non default $port = isset($parts['port']) && (($protocol === 'http://' && $parts['port'] !== 80) || ($protocol === 'https://' && $parts['port'] !== 443)) ? ':' . $parts['port'] : ''; // rebuild return $protocol . $parts['host'] . $port . $parts['path'] . $query; } private static function shouldRetainParam($param) { foreach (self::$DROP_QUERY_PARAMS as $drop_query_param) { if (strpos($param, $drop_query_param . '=') === 0) { return false; } } return true; } /** * Build the multipart body for file uploaded request. * @param array $params Parameters for the request * @return string */ private static function buildHttpMultipartBody($params) { $body = ''; $pairs = array(); self::$boundary = $boundary = md5('BAIDU-PHP-SDK-V2' . microtime(true)); foreach ($params as $key => $value) { if ($value{0} == '@') { $url = ltrim($value, '@'); $content = file_get_contents($url); $array = explode('?', basename($url)); $filename = $array[0]; $body .= '--' . $boundary . "\r\n"; $body .= 'Content-Disposition: form-data; name="' . $key . '"; filename="' . $filename . '"'. "\r\n"; $body .= 'Content-Type: ' . self::detectMimeType($url) . "\r\n\r\n"; $body .= $content . "\r\n"; } else { $body .= '--' . $boundary . "\r\n"; $body .= 'Content-Disposition: form-data; name="' . $key . "\"\r\n\r\n"; $body .= $value . "\r\n"; } } $body .= '--' . $boundary . '--'; return $body; } /** * Tries to detect MIME type of a file * * The method will try to use fileinfo extension if it is available, * deprecated mime_content_type() function in the other case. If neither * works, default 'application/octet-stream' MIME type is returned * * @param string filename * @return string file MIME type */ private static function detectMimeType($filename) { // finfo extension from PECL available if (function_exists('finfo_open')) { if (!isset(self::$fileinfoDb)) { self::$fileinfoDb = finfo_open(FILEINFO_MIME); } if (self::$fileinfoDb) { $info = finfo_file(self::$fileinfoDb, $filename); } } // (deprecated) mime_content_type function available if (empty($info) && function_exists('mime_content_type')) { $info = mime_content_type($filename); } return empty($info)? 'application/octet-stream': $info; } } /* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */