file_get_contents实现数据Post数据方法
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实例代码如下:
<?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测试数据' , 'submit' => 'submit' , ); echo post( 'http://localhost/5-5/request_post_result.php' , $data ); ?>接收数据,request_post_result.php 接收经过post的数据,php代码如下:
<?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 ; }查看更多关于file_get_contents实现数据Post数据方法 - php函数的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did30870