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\r\n" . "Content-length: $content_length\r\n", '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
)
)
还不会,有大招,关注下方公众号直接提问就可以了哈