Home > Backend Development > PHP Tutorial > PHP uses stream_context_create() to simulate POST/GET request method, streamcontext_PHP tutorial

PHP uses stream_context_create() to simulate POST/GET request method, streamcontext_PHP tutorial

WBOY
Release: 2016-07-12 08:55:30
Original
1185 people have browsed it

PHP uses stream_context_create() to simulate POST/GET requests, streamcontext

This article describes the example of PHP using stream_context_create() to simulate POST/GET requests. Share it with everyone for your reference, the details are as follows:

Sometimes, we need to simulate POST/GET and other requests on the server side, that is, to implement the simulation in the PHP program. How to do it? In other words, in a PHP program, if you are given an array, how do you POST/GET this array to another address? Of course, it's easy to do it using CURL, but what if you don't use the CURL library? In fact, there is already a related function implemented in PHP, and this function is stream_context_create() that I will talk about next.

Show you the code directly, this is the best way:

$data = array(
    'foo'=>'bar', 
    'baz'=>'boom', 
    'site'=>'localhost', 
    'name'=>'nowa magic'); 
$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 * 60 // 超时时间(单位:s)
    )
);
$url = "http://localhost/test2.php";
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;

Copy after login

The code for http://localhost/test2.php is:

$data = $_POST;
echo '<pre class="brush:php;toolbar:false">';
print_r( $data );
echo '
';
Copy after login

The running result is:

Array
(
  [foo] => bar
  [baz] => boom
  [site] => localhost
  [name] => nowa magic
)

Copy after login

Some key points:

1. The above program uses the http_build_query() function. If you need to know more, you can refer to the previous article "How PHP uses http_build_query() to construct a URL string".

2. stream_context_create() is used to create context options for opening files, such as accessing with POST, using a proxy, sending headers, etc. Just create a stream. Let’s give another example:

$context = stream_context_create(array( 
    'http' => array( 
        'method' => 'POST', 
        'header' => sprintf("Authorization: Basic %s\r\n", base64_encode($username.':'.$password)). 
        "Content-type: application/x-www-form-urlencoded\r\n", 
        'content' => http_build_query(array('status' => $message)), 
        'timeout' => 5, 
    ), 
)); 
$ret = file_get_contents('http://twitter.com/statuses/update.xml', false, $context); 

Copy after login

3. The context options created by stream_context_create can be used for both streams and file systems. It is more useful for functions like file_get_contents, file_put_contents, and readfile that operate directly on file names without file handles. Adding headers to stream_context_create is only part of the function. You can also define proxies, timeouts, etc. This makes the function of accessing the web not weaker than curl.

4. The function of stream_context_create(): Create and return a text data stream and apply various options. It can be used for timeout settings, proxy servers, request methods, and header information settings of fopen(), file_get_contents() and other processes. .

5. stream_context_create can also solve file_get_contents timeout processing by adding the timeout option:

$opts = array(
  'http'=>array(
  'method'=>"GET",
  'timeout'=>60,
 )
);
//创建数据流上下文
$context = stream_context_create($opts);
$html =file_get_contents('http://localhost', false, $context);
//fopen输出文件指针处的所有剩余数据:
//fpassthru($fp); //fclose()前使用

Copy after login

Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", "Introduction to PHP basic syntax", "php operation office documentation" Summary of skills (including word, excel, access, ppt)", "Summary of php date and time usage", "Introduction to php object-oriented programming tutorial", "Summary of php string (string) usage", "Introduction to php mysql database operation" Tutorial" and "Summary of Common PHP Database Operation Skills"

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • Usage examples of PHP stream_context_create() function
  • Analysis of the role and usage of PHP stream_context_create()
  • Breakthrough in php Anti-leeching measures based on HTTP_REFERER (stream_context_create)
  • Use PHP fsockopen to simulate POST/GET method of transmitting data
  • php custom class fsocket to simulate post or get request method
  • Use PHP Socket programming to simulate Http post and get requests
  • Sample code for php to use socket to simulate post or get in http to submit data
  • php simulates POST|GET operation implementation code
  • PHP CURL simulates GET and POST function codes

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1117036.htmlTechArticlePHP uses stream_context_create() to simulate POST/GET request method, streamcontext This article describes the example of PHP using stream_context_create() to simulate POST/GET request method. Share it with everyone...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template