Home >Backend Development >PHP Problem >How does the php server respond to post requests?

How does the php server respond to post requests?

尚
Original
2019-10-21 11:32:245552browse

How does the php server respond to post requests?

Simulate post request:

<?php
// 建立连接
$curl = curl_init();
//设置
$url = &#39;localhost&#39;;
curl_setopt($curl, CURLOPT_URL, $url);
# 设置开启post请求
curl_setopt($curl, CURLOPT_POST, $url);
$post_data = array(
    &#39;user_name&#39; => &#39;admin&#39;,
    &#39;user_pwd&#39; => &#39;123456&#39;
    );
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
//发送
var_dump(curl_exec($curl));
//关闭
curl_close($curl);

php responds to post request:

CURLOPT_RETURNTRANSFER: Whether to output the response directly or process it in the form of a return value
Process the response data in the form of a return value:

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

POST file upload

Post data uses the file address, use the @ sign before the file instead of the string

$post_data = array(&#39;image&#39; => &#39;@c:/1.jpg&#39;);

Handle session COOKIE

CURLOPT_COOKIEFILE: Whether to send cookies

CURLOPT_COOKIEJAR: Specify the storage location of cookie variables set by the storage server

curl_setopt($curl, CURLOPT_COOKIEFILE, true);
curl_setopt($curl, CURLOPT_HEADER, &#39;c:/cookie.txt&#39;);

Processing response headers

CURLOPT_HEADER: Whether to obtain response header data

Get response header data:

curl_setopt($curl, CURLOPT_HEADER, true);

Operation response

Operation response header:

header() function

json:header("Content-type: application/json");
(ie6:header("Content-type: text/json");) picture :header('Content-Type:image/jpeg');, header('Content-Type:image/png'); etc.; encoding: header("Content-type:text/html;Charset=utf-8") ;

Operation response body

Any output is the response body. (echo, print, var_dump, all HTML code outside PHP tags)

Control browser cache

header(&#39;Expires: &#39; . gmdate(&#39;D, d M Y H:i:s&#39;, time()+5) . &#39; GMT&#39;);

Expires: Expiration date (GMT: Greenwich Mean Time)

gmdate() Format the timestamp to Greenwich mean time

self";

Recommended reading: php server

The above is the detailed content of How does the php server respond to post requests?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn