zoukankan      html  css  js  c++  java
  • PHP POST请求同时获取结果和header信息

        /**
         * # +========================================================================
         * # | - @name        测试宏泰
         * # | - @author     cq <just_leaf@foxmail.com> 
         * # | - @copyright zmtek 2018-11-12
         * # +------------------------------------------------------------------------
         * # | - 1.初始化传参值页面
         * # +========================================================================
         */
        public function pushht() {
            
            $url     = C('URL').'/login';
            
            $data     = array(
                'name'         => 'zmtek' ,
                'password'     => md5('123456')
            );
            
            $result = $this -> _request($url,$data);
            var_dump($result);
        } 
        
        /**
         * # +========================================================================
         * # | - @name        请求
         * # | - @author     cq <just_leaf@foxmail.com> 
         * # | - @copyright zmtek 2018-11-12
         * # +------------------------------------------------------------------------
         * # | - 1.初始化传参值页面
         * # +========================================================================
         */
        private function _request($url,$data) {
            
            ksort($data);
            $requestdata     = http_build_query($data);
            $content_length = strlen($requestdata);
            $options         = array(
                'http' => array(
                    'method' => 'POST',
                    'header' =>
                    "Content-type: application/x-www-form-urlencoded
    " .
                    "Content-length: $content_length
    ",
                    'content'=> $requestdata
                )
            );
            $server              = file_get_contents($url, false, stream_context_create($options));
            $result['result']    = json_decode($server,true);
            $header = array();
            foreach($http_response_header as $v) {
                list($k,$v) = explode(":", $v);
                $header[$k] = $v?trim($v):trim($k);
            }
            
            $result['header']    = $header;
            return $result;
        }

     Array
    (
        [result] => Array
            (
                [status] => 200
                [message] => 登录成功
                [data] => Array
                    (
                    )
    
            )
    
        [header] => Array
            (
                [HTTP/1.1 201 Created] => HTTP/1.1 201 Created
                [X-Powered-By] => Express
                [Access-Control-Allow-Origin] => *
                [token] => eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1dWlkIjoiMiIsIm5hbWUiOiJ6bXRlayIsImlhdCI6MTU2OTU1NDU1OSwiZXhwIjoxNTY5NjQwOTU5fQ.MUAVCZt5yQacazkxHe_E66mH-u7lT5BsC_Uh7quCt4k
                [Content-Type] => application/json; charset=utf-8
                [Content-Length] => 49
                [ETag] => W/"31-WRHlgk6TGOOrDY31UATfWbsZPcg"
                [Vary] => Accept-Encoding
                [Set-Cookie] => mimc=s%3AY8UnavpxMG2-_Ia0O8oydTgS_tGEFk2B.qIhZRosJJcZvUEDagqm%2FQw3Isu0NJv%2FAIssieldJTNo; Path=/; HttpOnly
                [Date] => Fri, 27 Sep 2019 03
                [Connection] => close
            )
    
    )
  • 相关阅读:
    测试
    mysql数据库 select语句全集
    Markdown文本的书写格式详解--有道云笔记
    mysql数据忘记库密码
    最新版mysql基本命令操作
    Python从入门到放弃
    第二阶段冲刺
    周总结15
    找水王
    用户体验评价
  • 原文地址:https://www.cnblogs.com/leaf-cq/p/11596826.html
Copyright © 2011-2022 走看看