Home > Article > Backend Development > PHP simulates form post request to implement login
The project needs to write a program that does not require form submission. I checked a lot of information and finally used CURL to simulate post submission. The sample program is as follows:
index.php
<?php header("Content-type:text/html;charset=utf-8"); $stuid = "201300301013"; $pwd = "111336"; $uri = "127.0.0.1/login.php";//这里换成你服务器的地址 // 参数数组 $data = array (
'stuid' => $stuid, 'pwd' => $pwd ); $ch = curl_init (); //初始化curl curl_setopt ( $ch, CURLOPT_URL, $uri ); curl_setopt ( $ch, CURLOPT_POST, 1 ); //使用post请求 curl_setopt ( $ch, CURLOPT_HEADER, 0 ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data); //提交数据 curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, true); //重定向地址也输出 $return = curl_exec ( $ch ); //得到返回值 curl_close ( $ch ); //关闭 print_r($return); //输出返回值 ?>login.php
<?php $user = $_POST['stuid']; $pwd = $_POST['pwd'];  
/* 这里对得到的数据进行处理 */ echo "成功!"; //最后输出结果 ?>
This is just a simple example. There are many things about curl that need to be studied. If you use PHP's curl to write a crawler, the mechanism is not very good. It is recommended to use java to implement the crawler program.
My own experience, for reference only!
Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces the PHP simulation form post request to realize login, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.