Home Backend Development PHP Tutorial How to send GET and POST requests through file_get_contents()? (Method introduction)

How to send GET and POST requests through file_get_contents()? (Method introduction)

Jul 20, 2020 pm 04:57 PM
file_get_contents() get request post request

How to send GET and POST requests through file_get_contents()? (Method introduction)

The server performs HTTP requests. What everyone often uses is CURL. The curl tool is indeed a good data file transfer tool. In addition, there are other tools that can achieve this. Function?

Now I will introduce you to a very common toolfile_get_content()

Nani, isn’t this a PHP file operation function??? Can GET POST requests also be implemented? ? ?

I can tell you clearly here that it is absolutely possible! ! !

Without further ado, let’s get straight to the code. If you have any questions, please call me. Remember to bring medical expenses

1. [GET request]

$data = array( 'name'=>'zhezhao','age'=>'23');
$query = http_build_query($data); 
$url = 'http://localhost/get.php';//这里一定要写完整的服务页面地址,否则php程序不会运行 
$result = file_get_contents($url.'?'.$query);
Copy after login

2.[ POST request】

$data = array('user'=>'jiaxiaozi','passwd'=>'123456');
$requestBody = http_build_query($data);
$context = stream_context_create(['http' => ['method' => 'POST', 'header' => "Content-Type: application/x-www-form-urlencoded\r\n"."Content-Length: " . mb_strlen($requestBody), 'content' => $requestBody]]);
$response = file_get_contents('http://server.test.net/login', false, $context);
Copy after login

Related tutorial recommendations: "PHP Tutorial"

The above is the detailed content of How to send GET and POST requests through file_get_contents()? (Method introduction). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
Send POST request with form data using http.PostForm function Send POST request with form data using http.PostForm function Jul 25, 2023 pm 10:51 PM

Use the http.PostForm function to send a POST request with form data. In the http package of the Go language, you can use the http.PostForm function to send a POST request with form data. The prototype of the http.PostForm function is as follows: funcPostForm(urlstring,dataurl.Values)(resp*http.Response,errerror)where, u

How to send POST request and get response using http.Post function in golang How to send POST request and get response using http.Post function in golang Nov 18, 2023 am 08:05 AM

How to use the http.Post function in golang to send a POST request and get the response. When using golang for network programming, the http package is an important module we often use. Among them, the http.Post function is a very practical function that can easily send POST requests and obtain response results. The following will introduce the specific steps and code examples on how to use the http.Post function to send a POST request and obtain a response. Step 1: Import the http package in the code first

How to use the urllib.request.urlopen() function to send a POST request in Python 3.x How to use the urllib.request.urlopen() function to send a POST request in Python 3.x Jul 31, 2023 pm 07:10 PM

How to use the urllib.request.urlopen() function in Python3.x to send a POST request. In network programming, it is often necessary to send a POST request through the HTTP protocol to interact with the server. Python provides the urllib.request.urlopen() function to send various HTTP requests, including POST requests. This article will introduce in detail how to use urllib.request.urlop

How to handle POST request in FastAPI and return JSON response How to handle POST request in FastAPI and return JSON response Jul 29, 2023 pm 03:08 PM

How to handle POST requests and return JSON responses in FastAPI FastAPI is a modern web framework that is fast (high performance), easy to use, and based on standard Python type hints. It has strong asynchronous support and can easily handle high concurrency situations. In FastAPI, we can use concise code to handle POST requests and return JSON responses. This article will describe how to accomplish this task in FastAPI and provide corresponding code examples. First, we need to create

How to disable get request using php How to disable get request using php Mar 24, 2023 pm 05:09 PM

PHP is a popular server-side programming language that is widely used to build web applications. When developing web applications using PHP, it is very important to ensure security. Since HTTP requests include GET and POST, and since the GET request contains the request parameters sent by the client in the URL, the security of the web application can be enhanced by prohibiting the GET request. In this article, we will discuss how to suppress GET requests through PHP.

Correct usage of POST request in PHP Correct usage of POST request in PHP Mar 27, 2024 pm 03:15 PM

The use of POST requests in PHP is a common operation in website development. Data can be sent to the server through POST requests, such as form data, user information, etc. Proper use of POST requests can ensure data security and accuracy. The following will introduce the correct usage of POST requests in PHP and provide specific code examples. 1. Basic principles of POST requests in PHP In PHP, the data submitted through the POST method can be obtained by using the $_POST global variable. The POST method converts the form number into

Learn the net/http.Post function in the Go language documentation to send a POST request Learn the net/http.Post function in the Go language documentation to send a POST request Nov 04, 2023 am 11:39 AM

Learning network programming in Go language is a very important part, and sending POST requests is an indispensable part. This article will introduce how to use the net/http.Post function in the Go language documentation to send a POST request, including specific code examples. First, we need to understand what a POST request is. It is a request method for sending data to the server. Unlike GET requests, POST requests can send more data and do not expose the data in the URL. Normally, we use P

PHP Warning: file_get_contents():Solution PHP Warning: file_get_contents():Solution Jun 24, 2023 pm 11:15 PM

PHP is a widely used programming language that is often used for web development. In the process of using PHP, we often encounter various problems. One of the common problems is "PHPWarning: file_get_contents()". When we use the file_get_contents() function to read a file, the following warning message sometimes appears in the PHP log: PHPWarning:file_get_contents():

See all articles