PHP uses the curl function background to remotely log on to the Zhengfang educational administration system

Source: Internet
Author: User
Tags curl

He finally figured out how to solve this problem last year, but the verification code must be filled in.

If you can automatically identify the verification code like 360, it will be okay. No more nonsense. Back to question

CURL is used here.

Design Concept: first log on to the page to obtain COOKIES, and then take the cookies to the server for verification code. Finally, all the information required by the server is provided.

(This kind of thinking completely simulates the browser's access to the page. The fundamental difference is that people are still machines)

The code is as follows: Copy code

Public function index (){
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url); // you can enter the correct URL.
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0 )");
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_file); // The path of the main cookie. This saves the cookie on the page.
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // automatically outputs the returned results
$ Response = curl_exec ($ ch );
Curl_close ($ ch );

Visit the homepage of the Zhengfang educational administration system. The first is to get the HTML on the page and the second is to get cookies.
Other questions? How to obtain HTML?

If you carefully observe the HTML of the homepage of the educational administration system, you will find many hidden fields, and these fields are changed.
So you can retrieve all the HTML and parse it, and then extract all the required fields.

If you do not see it, hidden fields are required for data submission and cannot contain many fields.

Page effect
Next I will discuss the most important question: how can I obtain the verification code?

At first, I want to directly

The code is as follows: Copy code

No? Although the verification code is provided, it does not belong to you. Therefore, when you log on remotely, the system will tell you that the verification code is incorrect.

How can I obtain my own verification code? At this time, we naturally think of cookies.

How does the server tell whether the browser request is the same person? Check the sessionid in cookies.

In this way. Take the cookies you obtained on the home page and ask the server for your verification code. Will it give an error?

The code is as follows: Copy code

Public function getImg ()
$ Url = "http: // xxxx/CheckCode. aspx ";
$ Filedir = SITE_PATH. "/TMP/Cookies ";
$ Cookie_file = $ filedir. "/cookie.txt ";
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0 )");
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie_file );//
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_TIMEOUT, "10 ");
Header ("Content-type: image/gif"); // The format varies depending on the image format. Note that
Echo curl_exec ($ ch );
Curl_close ($ ch );

Then you can call this method on other pages to obtain the verification code.

The PHP requests of CURL belong to two different threads than the PHP requests sent by the browser. Therefore, their cookies are shared differently.
In this way, you can log on, but do not assume that you have logged on. You can operate on all the functions, and you will be wrong. There is also a small detail ..

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.