HTTP中的表单请求的三种方式

1.file_get_contents/fopen方式

<?php
     $postData = array(
          //构造的数据
     );
$postData = http_build_query($postData);//处理引号相连
     $opts = array(
         'http'=>array(
         'method'=>"POST",
         'header'=>"Host:localhost\r\n".
                   "Content-type:application/x-www-form-urlencodeed\r\n".
                   "Content-length:".strlen($postData)."\r\n",
         'content' => $postData,
     $context = stream_context_create( $opts );

     1 . file_get_contents( http://xxxx/xxxx/xxxx.php,false,$context);


     2. $fp = fopen("http://xxx","r",false,$context);
         $fclose = fclose($fp);


2.CURL方式(需要开启扩展)
//1.开启会话     返回资源句柄
$ch = curl_init();
//2.会话传输选项设置
curl_setopt($ch,CURLOPT_URL,$url) //设置提交的网址
curl_setopt($ch,CURLOPT_POST,1)//设置数据提交方式
curl_setopt($ch,CURLOPT_POSTFIELDS,$postData)//设置提交的数据
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1)//提交之后,把数据返回为字符串
//3.执行会话
$output = curl_exec($ch);
//4.关闭会话
curl_close($ch);

3.SOCKET方式

<?php
$postData =array();
$postData = http_build_query($postData);
$fp = fsocketopen("localhost",80,$errno,$errorStr,5);
$request = "POST http://xxx HTTP/1.1\r\n ";
$request .= "Host:localhost\r\n";
$request .= "Content-type:application/x-www-form-urlencode\r\n";
$request .="Content-length:".strlen($postData)."\r\n\r\n";
$request .=$postData;
fwrite($fp,$request);
//想把请求数据读出来
while(!feof($fp)){
     echo fget($fp,1024);
}
fclose($fp);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值