PHP http_response_code Get or Set the HTTP response code
  M9aMEIE19lAW 2023年11月02日 36 0



PHP100 »  PHP教程 » Get or Set the HTTP response code


PHP http_response_code Get or Set the HTTP response code



http_response_code

(No version information available, might only be in SVN)

http_response_code — Get or Set the HTTP response code



说明


int http_response_code ([ int $response_code

If you pass no parameters then http_response_code will get the current status code. If you pass a parameter it will set the response code.



参数


response_code

response_code

<?php
 http_response_code(404);
?>



返回值

The current response code. By default the return value is int(200).



范例


Example #1 Examples using headers_list()


<?php

// Get the current default response code
var_dump(http_response_code()); // int(200)

 // Set our response code
http_response_code(404);

// Get our new response code
var_dump(http_response_code()); // int(404)
?>


以上例程会输出:



参见

  • header() - Send a raw HTTP header
  • headers_list() - Returns a list of response headers sent (or ready to send)




PHP模拟POST请求,获取response内容




1     /*
 2      * 模拟POST请求,获取response内容
 3      */
 4     protected function curl($url, $type, $header, $data) {
 5         $CURL_OPTS = array (
 6                 CURLOPT_CONNECTTIMEOUT => 10,
 7                 CURLOPT_RETURNTRANSFER => true,
 8                 CURLOPT_SSL_VERIFYPEER => false,
 9                 CURLOPT_TIMEOUT => 60,
10                 CURLOPT_USERAGENT => 'simulate submit' 
11         );
12         $opts = $CURL_OPTS;
13         $opts [CURLOPT_URL] = $url;
14         $opts [CURLOPT_CUSTOMREQUEST] = $type;
15         $header [] = 'Expect:';
16         $opts [CURLOPT_HTTPHEADER] = $header;
17         if ($type == 'POST' || $type == 'PUT') {
18             $opts [CURLOPT_POSTFIELDS] = $data;
19         }
20         
21         $ch = curl_init ();
22         curl_setopt_array ( $ch, $opts );
23         $result = curl_exec ( $ch );
24         if (curl_errno ( $ch )) {
25             die ( 'CURL error: ' . curl_error ( $ch ) );
26         }
27         curl_close ( $ch );
28         return $result;
29     }




参数:

$header = array (
     'Content_type: application/x-www-form-urlencoded' 
   );$data="param1=qwer¶m2=asdf¶m3=jkl";


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  38gcbVXUBcLA   2023年11月26日   25   0   0 服务器htmlHTTP
  L83A5jZvvg3Q   2023年11月22日   25   0   0 长连接TCPHTTP
  38gcbVXUBcLA   2023年11月24日   25   0   0 服务器客户端HTTP
  KRsXEGSB49bk   2023年11月24日   49   0   0 TCPHTTP首部
  eHipUjOuzYYH   2023年12月06日   29   0   0 nginxHTTP
  L83A5jZvvg3Q   2023年11月22日   17   0   0 HTTP重定向字段
  O3FLR83nwSoe   2023年11月22日   29   0   0 nginxHTTPbash
  L83A5jZvvg3Q   2023年11月22日   27   0   0 客户端HTTP字段
  D04qQxYJZ4A6   2023年11月19日   38   0   0 mysqlredisphp
  38gcbVXUBcLA   2023年11月25日   29   0   0 服务器客户端HTTP
  KRsXEGSB49bk   2023年11月22日   28   0   0 TCPHTTP首部