The CURL application of the public platform. This article will introduce you to CURL application instances on the public platform. if you encounter such problems, please refer to them. Curl has been widely used in the past few days. This article will introduce you to curl application instances on the public platform. if you encounter such problems, please refer to them.
Curl is used to transmit data through a simulated browser. it supports many protocols such as HTPP, HTTPS, and FTP .... Many protocols are useful when collecting and simulating users to perform some operations.
There are four steps to use CURL:
1. initialize the URL
2. set request parameters (COOKIE, HEAD ...)
3. execute the request
4. close Resources
Let's start with a simple collection. generally, we can use the file_get_contents () function to retrieve the content of a webpage. now we can capture the content of a webpage through CURL.
| The code is as follows: |
|
|
$ Ch = curl_init (); // initialize a resource
Curl_setopt ($ ch, CURLOPT_URL, "http://www.mapenggang.com"); // set the page we want to get
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // disable direct output
$ String = curl_exec ($ ch );
Curl_close ($ ch ); |
Note: The focus is on the second parameter of the curl_setopt () function (below are some common materials)
In this way, we can get the content of this webpage. if only CURL is used to do this, it will be useless. CURL can actually be used for more amazing things.
Recently I just arrived at a new entrepreneurial company (Nima, it is really hard to choose this company, because there are several offers in his hand, but the salary offered here is very low because of entrepreneurial companies. I don't know why I chose this company. my friends are puzzled to choose this company. in fact, I don't know why I chose this company, the salary of other companies is probably twice that of the company. I hope this time I have not chosen the right one. otherwise. all the things that I want to do will have such nonsense.) what I want to do is develop popular public platforms, because currently there are few open interfaces, so there are very few things you get through the interface (Nima, Jack Ma, when are you going to give more excuses !), However, the official operating platform does not have data in many interfaces, so we need to find some data on our own. well, the main character plays CURL.
First, the public platform can be accessed only after login. I should log on first (nonsense). first, I need to capture packets and analyze the data that can be submitted normally. I will not be able to submit the data here (the blog is on the bae platform, the editor does not have time to ignore him, so it is not easy to understand). through packet capture analysis, it is found that the public platform uses ajax to log on, and the password has been encrypted by md5 before submission (it seems that it should be called md5 hash, and the standard MD5 hash should be 128 bits, however, for the convenience of storage and transmission, there are now a maximum of 32 and 16 bits. I just learned, I am sorry). another important point is that the public platform uses https protocol for login. The best thing is that no verification code is required, sogay. Otherwise, it will be difficult to analyze it. Come on !!!!
| The code is as follows: |
|
|
$ Password = md5 ($ password); // because md5 encryption is detected during packet capture, we will encrypt the password in advance.
$ Post = "username = {$ username} & pwd = {$ password} & f = json & imgcode = ";
$ LoginUrl = "https://mp.weixin.qq.com/cgi-bin/login? "; // Logon address
// The header information here must be set. you can obtain the header information when you capture the packet.
$ HeaderArray = array (
'Accept: application/json, text/javascript ,*/*',
'Content-Type: application/x-www-form-urlencoded ',
'Referer: https://mp.weixin.qq.com /'
);
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ loginUrl );
// Check the certificate Source. 0 indicates that the certificate validity check is blocked.
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false );
// Check whether the SSL encryption algorithm exists from the certificate
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // disable direct output
Curl_setopt ($ ch, CURLOPT_POST, 1); // submit data using post
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post); // sets the data submitted by post.
Curl_setopt ($ ch, CURLOPT_USERAGENT, 'mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/100'); // Set User proxy
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ headerArray); // Set the header information
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_file); // Set the cookie storage directory, which is very important here, you know (you do not store cookies, you think you are Ma Huateng !)
$ LoginData = curl_exec ($ ch); // token is returned here, which needs to be processed.
// Obtain the token value
$ LoginData = json_decode ($ loginData, true );
$ Token = explode ("=", $ loginData ['errmsg ']);
$ Token = array_pop ($ token );
Echo "system logon successful
";
Curl_close ($ ch );
|
The above is the code for logging on to the public platform. it has been tested to be very easy to use + _ +.
There have been many contacts with the public platform over the past few days. this is only the first step in the long journey. I will share with you how to map the fakeid and openid, you can display complete user information on your own platform (as far as I know, there is no good solution on the Internet for ing between fakeid and openid, after several days of hard work, we can now find it quite difficult, and the existing users cannot be right (in fact, this theory is acceptable, but it is not mentioned in detail, in fact, there is no time to do this, and there is a chance to try it, but the implementation requires the support of the existing system, that is, your current system must record the chat record with the use of (what do I say? !))).
The PE instructor, you said that you are not good enough to take your PE class and didn't come to teach us the language. in the past few days, there have been too many things that can be written, so I thought it was a bit messy, CURL is mainly used for a few days. so today, when you click CURL, you just need to write the information you have logged on to the public platform. CURL ends here and may be written later.