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
            )
    
    )
  • 相关阅读:
    smtplib.py
    淘宝链接中的spm参数
    with 上下文管理
    python RecursionError: maximum recursion depth exceeded while calling
    GraphQL两年实战
    Exception 异常处理
    Simple decorator that intercepts connection errors and ignores these if settings specify this.
    namedtuple
    服务治理在猫眼娱乐的演进之路
    路由、限流、熔断 微服务治理
  • 原文地址:https://www.cnblogs.com/leaf-cq/p/11596826.html
Copyright © 2011-2022 走看看