访问地址http://127.0.0.1/everyday/ssl/curl.php 代码如下
<?php
/** curl 获取 https 请求
* @param String $url 请求的url
* @param Array $data 要发送的数据
* @param Array $header 请求时发送的header
* @param int $timeout 超时时间,默认30s
*/
function curl_https($url, $data=array(), $header=array(), $timeout=30){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 信任任何证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 0 域名存在与否都不验证了(1检查证书中是否设置域名)
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$response = curl_exec($ch);
if($error=curl_error($ch)){
die($error);
}
curl_close($ch);
return $response;
}
$url = 'https://localhost/everyday/ssl/login.php';
$data = array('username'=>'admin','password'=>'admin888');
$header = array();
$response = curl_https($url, $data, $header, 5);
echo $response;
?>
登陆地址https://localhost/everyday/ssl/login.php
<form action="#" method="post">
用户名:<input type="text" name="username" />
<br />
密码:<input type="password" name="password" />
<br />
<input type="submit" name="submit" value="登陆" />
</form>
<?php
if(isset($_POST["username"])){
$user = $_POST["username"];
$psw = $_POST["password"];
if($user == "admin" && $psw == "admin888"){
echo "登陆成功!";
} else {
echo "户名或密码为空或者错误!";
}
}
?>