File_get_contents-PHP source code

Source: Internet
Author: User
The ec (2); file_get_contents () function reads the entire file into a string. Like file (), the difference is that file_get_contents () reads the file into a string. The file_get_contents () function is the preferred method for reading the file content into a string. If supported by the operating system, the memory ing technology will be used to enhance the performance. Syntax file_get_contents (path, inclu script ec (2); script

The file_get_contents () function reads the entire file into a string.

Like file (), the difference is that file_get_contents () reads the file into a string.

The file_get_contents () function is the preferred method for reading the file content into a string. If supported by the operating system, the memory ing technology will be used to enhance the performance.

Syntax
File_get_contents (path, include_path, context, start, max_length) parameter description
Path is required. Specifies the file to be read.
Optional. If you want to search for files in include_path, you can set this parameter to "1 ".
Context is optional. Specifies the file handle environment.

Context is a set of options that can modify the behavior of a stream. If null is used, this parameter is ignored.

Start is optional. Specifies the location where the file starts reading. This parameter is newly added to php tutorial 5.1.
Max_length is optional. The number of bytes to read. This parameter is newly added in php 5.1.


Php code

Function post ($ url, $ post = null)
{
$ Context = array ();

If (is_array ($ post ))
{
Ksort ($ post );

$ Context ['http'] = array
(
'Method' => 'post ',
'Content' => http_build_query ($ post ,'','&'),
);
}

Return file_get_contents ($ url, false, stream_context_create ($ context ));
}

$ Data = array
(
'Name' => 'test ',
'Email '=> 'test @ gmail.com ',
'Submit '=> 'submit ',
);

Echo post ('HTTP: // localhost/5-5/request_post_result.php', $ data );
?>
Receive data:

Request_post_result.php receives post data:
Php code
Echo $ _ post ['name'];
Echo $ _ post ['email '];
Echo $ _ post ['submit '];
Echo "fdfd ";
?>


Instance 2

/**
* Other Versions
* Usage:
* $ Post_string = "app = request & version = beta ";
* Request_by_other ('HTTP: // facebook.cn/restserver.php', then post_string );
*/
Function request_by_other ($ remote_server, $ post_string ){
$ Context = array (
'Http' => array (
'Method' => 'post ',
'Header' => 'content-type: application/x-www-form-urlencoded'. "rn ".
'User-agent: jimmy's post example beta '. "rn ".
'Content-length: '. strlen ($ post_string) + 8,
'Content' => 'mypost = '. $ post_string)
);
$ Stream_context = stream_context_create ($ context );
$ Data = file_get_contents ($ remote_server, false, $ stream_context );
Return $ data;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.