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);
发布了14 篇原创文章 · 获赞 21 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/wanghongbiao1993/article/details/78111749