将 cURL 替换为 CakePHP 3 Http 客户端后的'Invalid Client'响应



我正在尝试更新我的 CakePHP 3.5 控制器以使用 Cake Http 客户端而不是以下 cURL 代码:

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 clientPOST的标头为:

   $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代码中使用的数组相同。任何想法将不胜感激。

问题不在于CakePHP Client,我发现我在更新过程中不小心更改了调用函数。

相关内容

最新更新