Home>Article>Backend Development> PHP uses file_get_contents to send http request function with simple function

PHP uses file_get_contents to send http request function with simple function

不言
不言 Original
2018-05-02 09:54:59 2418browse

This article mainly introduces the implementation of PHP using file_get_contents to send http requests. The function is simple and has certain reference value. Now I share it with everyone. Friends in need can refer to it.

The examples in this article describe the use of PHP file_get_contents sends http request function. Share it with everyone for your reference, the details are as follows:

It is easy to use CURL to simulate POST/GET and other requests on the server side (for example, the previous article "php uses CURL to simulate GET and POST to submit and obtain from the WeChat interface" Data method"), so what should we do if we don't use the CURL library?

$data = array( 'test'=>'bar', 'baz'=>'boom', 'site'=>'www.nimip.com', 'name'=>'nimip.com'); $data = http_build_query($data); //$postdata = http_build_query($data); $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type:application/x-www-form-urlencoded', 'content' => $data 'timeout' => 60 // 超时时间(单位:s) ) ); $url = "http://www.testweb.com"; $context = stream_context_create($options); $result = file_get_contents($url, false, $context); echo $result;

The code for http://www.testweb.com is:

$data = $_POST; print_r( $data );

stream_context_create()Function: Create and return a text data stream and apply various options, which can be used forfopen(),file_get_contents()and other processes Special processes for timeout settings, proxy servers, request methods, and header information settings.

Related recommendations:

Analysis of how PHP uses new StdClass() to create an empty object

php uses gearman for task distribution

The above is the detailed content of PHP uses file_get_contents to send http request function with simple function. 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