


This article mainly introduces PHP's use of Curl to implement simulated login and data capture functions. It analyzes PHP's use of curl for login, verification, cookie operation and data capture and other related implementation techniques in the form of examples. Friends in need can refer to it. Next
The example in this article describes how PHP uses Curl to implement simulated login and data capture functions. Share it with everyone for your reference, the details are as follows:
Using PHP's Curl extension library can simulate login and capture some data that can only be viewed after logging in with a user account. The specific implementation process is as follows (personal summary):
1. First, you need to analyze the html source code of the corresponding login page to obtain some necessary information:
(1) The login page Address;
(2) Verification code address;
(3) Names and submission methods of each field that need to be submitted in the login form;
(4) Login form submission Address;
(5) In addition, you need to know the address of the data to be captured.
2. Get the cookie and store it (for websites that use cookie files):
$login_url = 'http://www.xxxxx'; //登录页面地址 $cookie_file = dirname(__FILE__)."/pic.cookie"; //cookie文件存放位置(自定义) $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $login_url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); curl_exec($ch); curl_close($ch);
3. Get the verification code and store it (for websites that use cookie files) Verification code website):
$verify_url = "http://www.xxxx"; //验证码地址 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $verify_url); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $verify_img = curl_exec($ch); curl_close($ch); $fp = fopen("./verify/verifyCode.png",'w'); //把抓取到的图片文件写入本地图片文件保存 fwrite($fp, $verify_img); fclose($fp);
Description:
Since the verification code cannot be recognized, I here The method is to capture the verification code image and store it in a local file, then display it on the html page in your project, let the user fill it in, wait for the user to fill in the account number, password and verification code, and click the submit button. Go to the next step.
4. Simulate submission of login form:
$ post_url = 'http://www.xxxx'; //登录表单提交地址 $post = "username=$account&password=$password&seccodeverify=$verifyCode";//表单提交的数据(根据表单字段名和用户输入决定) $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ post_url); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); //提交方式为post curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); curl_exec($ch); curl_close($ch);
5. Capture data:
$data_url = "http://www.xxxx"; //数据所在地址 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $data_url); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER,0); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); $data = curl_exec($ch); curl_close($ch);
So far, the page where the data is located has been captured and stored in the string variable $data.
It should be noted that what is captured is the html source code of a web page, which means that this string not only contains the data you want, but also contains many html tags and other things you don’t want. thing. So if you want to extract the data you need, you have to analyze the HTML code of the page where the data is stored, and then use string manipulation functions, regular matching and other methods to extract the data you want.
The above method is effective for general websites using http protocol. But if you want to simulate logging in to a website that uses https protocol, you need to add the following processing:
1. Skip https verification:
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
2. Use user agent:
$UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)'; curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);
Note: If you do not add these processes, the simulated login will not be successful. .
Using the above program to simulate logging into a website is generally successful, but in fact it still needs to be considered based on the specific circumstances of the simulated login website. For example: some websites have different encodings, so the pages you capture are garbled. In this case, you need to perform encoding conversion, such as: $data = iconv("gb2312", "utf-8",$data) ;
, convert gbk encoding to utf8 encoding. There are also some websites that have relatively high security requirements, such as online banking, which will put the verification code in an inline frame. In this case, you need to first crawl the page of the inline frame and then extract the address of the verification code from it. Go grab the verification code again. There are also some websites (such as online banking) that submit forms in js code. Before submitting the form, they will also do some processing, such as encryption, etc., so if you submit it directly, you will not be able to log in successfully. You must do it Submit after similar processing, but in this case, if you can know the specific operations performed in the js code, such as encryption, what the encryption algorithm is, you can perform the same processing as it does, and then submit the data, so It can also be successful. However, here comes the key point. If you don’t know what operations it performs at all, for example, it is encrypted, but you don’t know the specific encryption algorithm, then you will not be able to perform the same operation, and you will not be able to successfully simulate it. Logged in. A typical case in this regard is online banking. It uses the online banking control to perform some processing on the password and verification code submitted by the user before submitting the form in the js code. However, we have no idea what operations it performs, so we cannot simulate it. So if you think you can simulate logging into online banking after reading this article, then you are too naive. Can you simulate logging into the bank's website so easily? Of course, if you can crack the online banking controls, that's another matter. Having said that, why do I feel so deeply? Because I have encountered this problem. If I don’t talk about it, I will shed tears if I talk too much. . .
Related recommendations:
php uses gearman for task distribution
PHP uses zlib extension to achieve GZIP compression output
PHP uses Nginx to implement reverse proxy
##
The above is the detailed content of PHP uses Curl to implement simulated login and data capture function examples. For more information, please follow other related articles on the PHP Chinese website!

curl和Pythonrequests都是发送HTTP请求的强大工具。虽然curl是一种命令行工具,可让您直接从终端发送请求,但Python的请求库提供了一种更具编程性的方式来从Python代码中发送请求。将curl转换为Pythonrequestscurl命令的基本语法如下所示:curl[OPTIONS]URL将curl命令转换为Python请求时,我们需要将选项和URL转换为Python代码。这是一个示例curlPOST命令:curl-XPOSThttps://example.com/api

在Linux下更新curl版本,您可以按照以下步骤进行操作:检查当前curl版本:首先,您需要确定当前系统中安装的curl版本。打开终端,并执行以下命令:curl--version该命令将显示当前curl的版本信息。确认可用的curl版本:在更新curl之前,您需要确定可用的最新版本。您可以访问curl的官方网站(curl.haxx.se)或相关的软件源,查找最新版本的curl。下载curl源代码:使用curl或浏览器,下载您选择的curl版本的源代码文件(通常为.tar.gz或.tar.bz2

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

PHP8.1发布:引入curl多个请求并发处理近日,PHP官方发布了最新版本的PHP8.1,其中引入了一个重要的特性:curl多个请求并发处理。这个新特性为开发者提供了一个更加高效和灵活的方式来处理多个HTTP请求,极大地提升了性能和用户体验。在以往的版本中,处理多个请求往往需要通过创建多个curl资源,并使用循环来分别发送和接收数据。这种方式虽然能够实现目

从头到尾:如何使用php扩展cURL进行HTTP请求引言:在Web开发中,经常需要与第三方API或其他远程服务器进行通信。而使用cURL进行HTTP请求是一种常见而强大的方式。本文将介绍如何使用php扩展cURL来执行HTTP请求,并提供一些实用的代码示例。一、准备工作首先,确保php已安装cURL扩展。可以在命令行执行php-m|grepcurl查

在linux中,curl是一个非常实用的、用来与服务器之间传输数据的工具,是一个利用URL规则在命令行下工作的文件传输工具;它支持文件的上传和下载,是综合传输工具。curl提供了一大堆非常有用的功能,包括代理访问、用户认证、ftp上传下载、HTTP POST、SSL连接、cookie支持、断点续传等等。

PHPCurl中如何处理网页的301重定向?在使用PHPCurl发送网络请求时,时常会遇到网页返回的301状态码,表示页面被永久重定向。为了正确处理这种情况,我们需要在Curl请求中添加一些特定的选项和处理逻辑。下面将详细介绍在PHPCurl中如何处理网页的301重定向,并提供具体的代码示例。301重定向处理原理301重定向是指服务器返回了一个30

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

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