file_get_contents实现数据Post数据技巧
发布时间:2022-06-18 15:43:39 所属栏目:PHP教程 来源:互联网
导读:file_get_contents() 函数把整个文件读入一个字符串中,和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串. file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法,如果操作系统支持,还会使用内存映射技术来增强性能. 语
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.com', '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; } (编辑:我爱制作网_潮州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |