search
HomeBackend DevelopmentPHP TutorialPHP uses Curl to implement simulated login and data capture function examples

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!

Statement
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
PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP: The Foundation of Many WebsitesPHP: The Foundation of Many WebsitesApr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

Beyond the Hype: Assessing PHP's Role TodayBeyond the Hype: Assessing PHP's Role TodayApr 12, 2025 am 12:17 AM

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

What are Weak References in PHP and when are they useful?What are Weak References in PHP and when are they useful?Apr 12, 2025 am 12:13 AM

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

Explain the __invoke magic method in PHP.Explain the __invoke magic method in PHP.Apr 12, 2025 am 12:07 AM

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools