Home >Backend Development >PHP Tutorial >Detailed explanation of the steps to send http request using file_get_contents in PHP

Detailed explanation of the steps to send http request using file_get_contents in PHP

php中世界最好的语言
php中世界最好的语言Original
2018-05-16 11:39:232415browse

This time I will bring you a detailed explanation of the steps to send an http request using PHP file_get_contents. What are the precautions for using PHP file_get_contents to send an http request? Here is a practical case. Let’s take a look. one time.

It is easy to simulate POST/GET and other requests on the server side using CURL. So what should we do if we do not 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 of 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 can be used for special processes such as timeout settings, proxy servers, request methods, and header information settings for <a href="//m.sbmmt.com/wiki/1325.html" target="_blank">fopen</a>(), file_get_contents() and other processes.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the use of Yii2 framework data verification

Detailed explanation of the steps to use PDO transactions in PHP

The above is the detailed content of Detailed explanation of the steps to send http request using file_get_contents in PHP. 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