dongyi9330 2016-10-14 05:40 采纳率: 100%
浏览 40

使用请求PHP或任何其他codeigniter进行摘要式身份验证

I have used http://requests.ryanmccue.info/ and https://github.com/rmccue/Requests

I am using Request library, but any other library can also be suggested.

My code for 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);

            }
        }

But I am getting the following error:

int(401) string(28) "HTTP Digest: Access denied. "
  • 写回答

1条回答 默认 最新

  • doumindang2416 2016-10-21 12:59
    关注

    Using Curl PHP Now

    $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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?