Home  >  Article  >  Backend Development  >  PHP uses file_get_contents to send http requests

PHP uses file_get_contents to send http requests

不言
不言Original
2018-04-24 14:07:153258browse

The main content of this article is about PHP using file_get_contents to send http requests. It has a certain reference value. Now I share it with everyone. Friends in need can refer to it

Server-side simulation POST /GET and other requests, are easy to do using CURL, 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.nimip.com";  
$context = stream_context_create($options);  
$result = file_get_contents($url, false, $context);  
  
echo $result;

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

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

stream_context_create() Function: Create and return A text data stream and applying various options, which can be used for special processes such as timeout settings, proxy servers, request methods, and header information settings for processes such as fopen() and file_get_contents().

Related recommendations:

PHP uses qqwry.bat to obtain the actual address

How to solve the problem that php uses the file_get_contents method to grab web page data and the garbled code appears

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