Heim  >  Artikel  >  Backend-Entwicklung  >  PHP_CURL1之模拟POST登陆_PHP教程

PHP_CURL1之模拟POST登陆_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:37:131060Durchsuche

CURL简介:

CURL允许你与各种的服务器使用各种类型的协议进行连接和通讯,目前支持的协议包括:http、https、ftp、gopher、telnet、dict、file、ldap,同时也支持HTTPS认证、HTTP POST、HTTP PUT、 FTP 上传(这个也能通过PHP的FTP扩展完成)、HTTP 基于表单的上传、代理、cookies和用户名+密码的认证。(摘自手册)

总之 CURL 功能非常强大,能实现很多 file_get_contents 函数所不能实现的功能。

原理性的东西不再赘述,这里代码来说话。

CURL模拟登陆:

首先,在你的项目中建立两个文件,login.php(提交登陆)、validate.php(验证),代码清单:

login.php

<?php
header('Content-type:text/html;Charset=utf-8');
$user = 'lee';       //登陆用户名
$pass = '123456';    //登陆密码
$va_url = 'http://localhost/validate.php';            //验证的 url 链接地址
$post_fields = "loginname={$user}&loginpass={$pass}"; //post提交信息串
$curl = curl_init(); //初始化一个cURL会话,必有
//curl_setopt()函数用于设置 curl 的参数,其功能非常强大,具体看手册
curl_setopt($curl, CURLOPT_URL, $va_url);      //设置验证登陆的 url 链接
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0); //设置结果保存在变量中,还是输出,默认为0(输出)
curl_setopt($curl, CURLOPT_POST, 1);           //模拟post提交
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields); //设置post串
$data = curl_exec($curl);  //执行此cURL会话,必有
curl_close($curl);         //关闭会话
validate.php

<?php
header('Content-Type:text/html;Charset=utf-8');
if ($_POST['loginname'] == 'lee' && $_POST['loginpass'] == '123456') {
    echo '<script>alert("登陆成功!");</script>';
} else {
    echo '<script>alert("登陆失败!");</script>';
}

登陆成功,JS 弹出“登陆成功”;登陆失败,JS弹出“登陆失败”。

注:原创文章,转载请注明出处:http://blog.csdn.net/liruxing1715/article/details/18551621

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/735907.htmlTechArticleCURL简介: CURL允许你与各种的服务器使用各种类型的协议进行连接和通讯,目前支持的协议包括:http、https、ftp、gopher、telnet、dict、file、...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn