BaiduUtils::request PHP Method

request() public static method

Request for a http/https resource
public static request ( string $url, array $params = [], string $httpMethod = 'GET', boolean $multi = false ) : string | false
$url string Url to request
$params array Parameters for the request
$httpMethod string Http method, 'GET' or 'POST'
$multi boolean Whether it's a multipart POST request
return string | false Returns string if success, or false if failed
    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;
    }

Usage Example

Example #1
0
 public function getLoggedInUser($access_token)
 {
     $result = BaiduUtils::request(self::$BD_OAUTH2_ENDPOINTS['getLoginUser'], array('access_token' => $access_token, 'redirect_uri' => $this->redirectUri), 'GET');
     if ($result) {
         $result = json_decode($result, true);
         if (isset($result['error_msg'])) {
             BaiduUtils::setError($result['error_code'], $result['error_msg']);
             return false;
         }
         return $result;
     }
     return false;
 }
All Usage Examples Of BaiduUtils::request