使用file_get_contents提交http post

使用file_get_contents提交http post时需开启了allow_url_fopen。

1.先看一下目标网页(假设是http://localhost/response.php)

response.php

<?php  
echo "<pre>";  
print_r($_POST);  
print_r($_COOKIE);  
?>   

本文讲述的只是http post请求的发送,所以,目标页只是回显所收到的post和cookie

2.请求页

request.php

<?php  
$data = array("name" => 'tim',"content" => 'test');  
$data = http_build_query($data);  
$opts = array(  
  'http'=>array(  
    'method'=>"POST",  
    'header'=>"Content-type: application/x-www-form-urlencoded\r\n".  
              "Content-length:".strlen($data)."\r\n" .   
              "Cookie: foo=bar\r\n" .   
              "\r\n",  
    'content' => $data,  
  )  
);  
$cxContext = stream_context_create($opts);  
$sFile = file_get_contents("http://localhost/response.php", false, $cxContext);  
  
echo $sFile;  
  
?>   

这个文件首先使用stream_context_create()构造了一个http请求,然后使用file_get_contents发送出去,返回的结果是:

Array  
(  
    [name] => tim  
    [content] => test  
)  
Array  
(  
    [foo] => bar  
)   

所以上可以看出,只要你了解http协议,完全可以使用这两个函数构造出所有正常的http请求,比如代理,断点续传等...

来源:http://blogread.cn/it/article/269?f=sinat

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值