【问题标题】:Digest Authentication using Request PHP or any other in codeigniter使用请求 PHP 或 codeigniter 中的任何其他方法进行摘要身份验证
【发布时间】:2016-10-14 05:40:54
【问题描述】:

我用过 http://requests.ryanmccue.info/https://github.com/rmccue/Requests

我正在使用 Request 库,但也可以建议任何其他库。

我的 CodeIgniter 代码

class Home extends CI_Controller{
            public function index(){
                $this->load->library('PHPRequest');     
                $this->rest_client();
            }
            function rest_client(){
                $user = 'myusername';
                $pass = 'mypass';
                $BaseApiUrl  = 'myurl';

                $headers = array('Accept' => 'application/json');
                $options = array('auth' => new Requests_Auth_Basic(array($user, $pass)));
                $request = Requests::get($BaseApiUrl, $headers, $options);
                var_dump($request->status_code);
                var_dump($request->body);

            }
        }

但我收到以下错误:

int(401) string(28) "HTTP Digest: Access denied. "

【问题讨论】:

    标签: php codeigniter authentication digest


    【解决方案1】:

    现在使用 Curl PHP

    $options = array(
                    CURLOPT_URL            => $url,
                    CURLOPT_HEADER         => false,    
                    CURLOPT_VERBOSE        => true,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_SSL_VERIFYPEER => false,    // for https
                    CURLOPT_USERPWD        => $username . ":" . $password,
                    CURLOPT_HTTPAUTH       => CURLAUTH_DIGEST
    
            );
            $ch = curl_init();
            curl_setopt_array( $ch, $options );
            try {
              $raw_response  = curl_exec( $ch );
              // validate CURL status
              if(curl_errno($ch))
                  throw new Exception(curl_error($ch), 500);
              // validate HTTP status code (user/password credential issues)
              $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
              if ($status_code != 200)
                  throw new Exception("Response with Status Code [" . $status_code . "].", 500);
            } catch(Exception $ex) {
                if ($ch != null) curl_close($ch);
                throw new Exception($ex);
            }
            if ($ch != null) curl_close($ch);
            return json_decode($raw_response);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多