search
Homephp教程PHP源码file_get_contents实现数据Post数据方法

<script>ec(2);</script>

file_get_contents() 函数把整个文件读入一个字符串中。

和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。

file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法。如果操作系统支持,还会使用内存映射技术来增强性能。

语法
file_get_contents(path,include_path,context,start,max_length)参数 描述
path 必需。规定要读取的文件。
include_path 可选。如果也想在 include_path 中搜寻文件的话,可以将该参数设为 "1"。
context 可选。规定文件句柄的环境。

context 是一套可以修改流的行为的选项。若使用 null,则忽略。
 
start 可选。规定在文件中开始读取的位置。该参数是 php教程 5.1 新加的。
max_length 可选。规定读取的字节数。该参数是 php 5.1 新加的。


php代码

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); 
?>  
接收数据:

request_post_result.php  接收经过post的数据:
php代码
echo $_post['name']; 
echo $_post['email']; 
echo $_post['submit']; 
echo "fdfd"; 
?>  


实例二

/**
* 其它版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_other('http://facebook.cn/restserver.php',$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;
}

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools