php模拟表单提交的三种方法

没有前台数据时,怎么模拟post提交表单呢,大概有这么三种方法,file_get_contents,curl,socket,我们分别说明一下。

先交代一下背景,首先模拟的是一个留言提交,数据库只有三个字段
在这里插入图片描述
id是自定增长的所以我们是需要模拟提交title和cintent,后台处理就是向插入一条记录,
index.php

<?php
require_once "connect.php";
$title=$_POST['title'];
$content=$_POST['content'];

if($title!=null && $content!=null){
    $sql="insert into message (title,content) values(?,?)";
    $res=$pdo->prepare($sql);
    $res->bindValue(1,$title);
    $res->bindValue(2,$content);
    $re=$res->execute();
    if($re>0){
        echo "留言成功";
    }
}

其中connect.php就是连接数据库的文件,就不做详细说明了,下面开始模拟表单提交;

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 新加的。

一般我们都是用file_get_context()都是用来读取指定文件的内容,然后就是用第一个参数,今天我们说的是
模拟表单提交,我们直接看代码,再解释

<?php

$postData=array(
    'title'=>'go1',
    'content'=>'高并发协程',
    'submit'=>'发布',
    );
$postData=http_build_query($postData);
$ops=array(
    'http'=>array(
    'method'=>'POST',
    'header'=>
          "Content-Type: application/x-www-form-urlencoded\r\n".
          "Content-length:".strlen($postData)."\r\n",
    'content'=>$postData,
    )
);
$context=stream_context_create($ops);
file_get_contents("http://localhost/data/mianshi/http/index.php",false,$context);
//$fp=fopen("http://localhost/data/mianshi/http/index.php","r",false,$context);
//fclose($fp);

我们先来解释几个函数
http_build_query():生成 url-encoded 之后的请求字符串描述string
stream_context_create():创建数据流上下文
首先将要传递的数组封装成一个字符串,然后通过stream_context_create()函数将http请求的内容创建成流,传递给要访问的后台页面,从而模拟post提交。
后面注释的两行可以取代file_get_contents(“http://localhost/data/mianshi/http/index.php”,false,$context);,完成一样的功能。

curl方法

这是php的一个扩展。可以用来模拟post提交,主要是四个步骤:

初始化crul

参数设置

页面内容获取或操作

释放句柄

<?php
$url="http://localhost/data/mianshi/http/index.php";
$postData=array(
    'title'=>'我是curl',
    'content'=>'curl传递参数',
    'submit'=>'留言',
);
//出是一个curl会话
$ch=curl_init();
//设置相应的会话选项
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_PORT,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postData);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//获取页面读取到的内容
$output=curl_exec($ch);
//释放句柄
curl_close($ch);
echo $output;

socket方法

主要是靠下面这个函数实现:
fsockopen ( string $hostname [, int KaTeX parse error: Expected 'EOF', got '&' at position 18: …rt = -1 [, int &̲errno [, string &$errstr [, float $timeout = ini_get(“default_socket_timeout”) ]]]] ) : resource
初始化一个套接字连接到指定主机(hostname)
参数:
hostname
如果安装了OpenSSL,那么你也许应该在你的主机名地址前面添加访问协议ssl://或者是tls://,从而可以使用基于TCP/IP协议的SSL或者TLS的客户端连接到远程主机。

port
端口号。如果对该参数传一个-1,则表示不使用端口,例如unix://。

errno
如果传入了该参数,holds the system level error number that occurred in the system-level connect() call。

如果errno的返回值为0,而且这个函数的返回值为FALSE,那么这表明该错误发生在套接字连接(connect())调用之前,导致连接失败的原因最大的可能是初始化套接字的时候发生了错误。

errstr
错误信息将以字符串的信息返回。

timeout
设置连接的时限,单位为秒。

Note:

注意:如果你要对建立在套接字基础上的读写操作设置操作时间设置连接时限,请使用stream_set_timeout(),fsockopen()的连接时限(timeout)的参数仅仅在套接字连接的时候生效。

返回值:
fsockopen()将返回一个文件句柄,之后可以被其他文件类函数调用(例如:fgets(),fgetss(),fwrite(),fclose()还有feof())。如果调用失败,将返回FALSE。

<?php

$postData=array(
    'title'=>'socket',
    'content'=>'socket提交表单',
);
$postData=http_build_query($postData);
$fp=fsockopen("localhost",80,$errno,$errorStr,5);
$request="POST http://localhost/data/mianshi/http/index.php HTTP/1.1\r\n";
$request.="HOST:localhost\r\n";
$request.="Content-Type: application/x-www-form-urlencoded\r\n";
$request.= "Content-length:".strlen($postData)."\r\n\r\n";
$request.=$postData;
fwrite($fp,$request);
while(!feof($fp)){
    echo fgets($fp,1024);
}
fclose($fp);

初始化一个套接字连接到本机,在$request封装具体的请求信息,通过fwrite()函数将请求完成,并返回文件句柄,然后通过feof读出来。

建议大家如果有兴趣自己做一下,能体会的更深一些。

猜你喜欢

转载自blog.csdn.net/LYue123/article/details/88558083
今日推荐