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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
