Home Backend Development PHP Tutorial PHP uses Curl to implement simulated login and data capture function examples

PHP uses Curl to implement simulated login and data capture function examples

Apr 27, 2018 pm 03:51 PM
curl php Log in

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);
Copy after login

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);
Copy after login

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);
Copy after login

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);
Copy after login

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);
Copy after login

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);
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles