2017-10-20 56 views
0

我試圖更新我的CakePHP 3.5控制器,而不是使用下面的捲曲代碼蛋糕HTTP客戶端:「無效客戶的迴應

private function executeRequest($url, $parameters = array(), $http_header, $http_method) 
{ 

    $curl_options = array(); 

    switch($http_method){ 
     case self::HTTP_METHOD_GET: 
      $curl_options[CURLOPT_HTTPGET] = 'true'; 
      if (is_array($parameters) && count($parameters) > 0) { 
      $url .= '?' . http_build_query($parameters); 
      } elseif ($parameters) { 
      $url .= '?' . $parameters; 
      } 
      break; 
     case self:: HTTP_METHOD_POST: 
      $curl_options[CURLOPT_POST] = '1'; 
      if(is_array($parameters) && count($parameters) > 0){ 
      $body = http_build_query($parameters); 
      $curl_options[CURLOPT_POSTFIELDS] = $body; 
      } 
      break; 
     default: 
      break; 
    } 
    /** 
    * An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100') 
    */ 
    if(is_array($http_header)){ 
     $header = array(); 
     foreach($http_header as $key => $value) { 
      $header[] = "$key: $value"; 
     } 
     $curl_options[CURLOPT_HTTPHEADER] = $header; 
    } 

    $curl_options[CURLOPT_URL] = $url; 
    $ch = curl_init(); 

    curl_setopt_array($ch, $curl_options); 
    // Require SSL Certificate 

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
    //Don't display, save it on result 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    //Execute the Curl Request 
    $result = curl_exec($ch); 

    $headerSent = curl_getinfo($ch, CURLINFO_HEADER_OUT); 

    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 

    $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); 
    if ($curl_error = curl_error($ch)) { 
     throw new Exception($curl_error); 
    } 
    else { 
     $json_decode = json_decode($result, true); 
    } 
    curl_close($ch); 

    return $json_decode; 
} 

更新的代碼是:

private function executeRequest($url, $http_header, $http_method, $parameters = array()) 
{ 
    $htclient = new Client(); 
    $response = null; 

    switch($http_method){ 
     case self::HTTP_METHOD_GET: 
      if (is_array($parameters) && count($parameters) > 0) { 
      $params = http_build_query($parameters); 
      } elseif ($parameters) { 
      $params = $parameters; 
      } 
      $response = $htclient->get($url, ['q' => $params], ['headers' => $http_header]); 
      break; 
     case self::HTTP_METHOD_POST: 
      if(is_array($parameters) && count($parameters) > 0){ 
      $response = $htclient->post($url, $parameters, ['headers' => $http_header]); 
      } 
      break; 
     default: 
      break; 
    } 

    return $response->json; 

} 

當我嘗試使用POST函數,返回代碼爲error: invalid client。 爲POST標題是:

$http_header = array(
    'Accept' => 'application/json', 
    'Authorization' => $authorizationHeaderInfo, 
    'Content-Type' => 'application/x-www-form-urlencoded' 
    ); 

和內容陣列是:

$parameters = array(
    'grant_type' => $grant_type, 
    'code' => $code, 
    'redirect_uri' => $redirectUrl 
    ); 

這些都是在cURL代碼中使用的相同的陣列。 任何想法將不勝感激。

回答

0

問題不在於CakePHP Client,我發現在更新過程中意外更改了調用函數。