Home Backend Development PHP Tutorial CURL application on WeChat public platform_PHP tutorial

CURL application on WeChat public platform_PHP tutorial

Jul 13, 2016 am 10:48 AM
curl one time introduce about classmate platform application WeChat article

This article will introduce you to the CURL application examples on the WeChat public platform. If you encounter such problems, please refer to it.

In the past few days, I have been using curl a lot in my work. Curl simulates a browser to transmit data. It supports many protocols such as HTTP, HTTPS, FTP, etc., when collecting and simulating users to perform some operations. Very useful.
There are four main steps to using CURL:
1. Initialization URL
2. Set some parameters of the request (COOKIE, HEAD...)
3. Execute request
4. Close resources
Let’s talk about a simple collection first. Generally, when obtaining the content of a web page, it is most convenient for us to use the file_get_contents() function to obtain it. Now we use CURL to capture the content of a web page

The code is as follows Copy code
 代码如下 复制代码

$ch = curl_init();//初始化一个资源
       curl_setopt($ch,CURLOPT_URL,”http://www.mapenggang.com”);//设置我们要获取的网页
       curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//关闭直接输出
       $string= curl_exec($ch);
       curl_close($ch);

$ch = curl_init();//Initialize a resource curl_setopt($ch,CURLOPT_URL,”http://www.mapenggang.com”);//Set the web page we want to get ​​​ curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//Close direct output          $string= curl_exec($ch); curl_close($ch);


Note: The key point lies in the second parameter of the curl_setopt() function (there will be some commonly used information below)
In this way, we can get the content of this web page. If only CURL can do something, it will be overkill. CURL can actually be used to do more magical things.
I just recently joined a new entrepreneurial company (Nima, I’m really confused about choosing this company because I have several offers, but the salary here is very low, because it’s an entrepreneurial company. I don’t know. Why did I choose this company? Anyway, my friends were confused about why I chose this company. In fact, I don’t know why I chose this company. The salary of other companies is about twice that of this company. I hope I didn’t choose this company this time. Wrong, otherwise, you will feel like dying, after talking so much nonsense), what I am doing is the development of the WeChat public platform, which is relatively popular now, because WeChat currently has very few open interfaces, so there are very few things that can be obtained through the interfaces. (Nima, Brother Ma, when did you have an excuse to play more!), but there are many interfaces in the official operating platform that do not have data, so we need to find some data ourselves. Well, the protagonist comes on CURL.

First of all, you need to log in to access the public platform, so I will log in first (nonsense). First, I need to capture packets and analyze the normal submission data. I will not take screenshots here (the blog is on the bae platform, and the editor has not had time yet) Ignore him, it’s not easy to use). Through packet capture analysis, we found that WeChat’s public platform uses ajax to log in, and the password has been md5 encrypted before submission (it seems that the formal one should be called md5 hash, and it is standard The MD5 hash should be 128 bits, but for the convenience of storage and transmission, the most common ones now are 32 and 16 bits. I just learned about it, and I’m ashamed). Another very important point is that the WeChat public platform uses the https protocol for login. . The best part is that there is no verification code required, sogay. Otherwise, it would be a lot of trouble to analyze it at this point. Come on!!!

The code is as follows Copy code
 代码如下 复制代码


$password = md5($password);//因为刚才抓包发现是md5加密过的,所以这里我们提前把密码加密号


$post = "username={$username}&pwd={$password}&f=json&imgcode=";
$loginUrl = "https://mp.weixin.qq.com/cgi-bin/login?";//微信登录的地址

//这里的头信息都是必须要设置的,这些你都可以在刚才抓包的时候获取到


$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);
// 对认证证书来源的检查,0表示阻止对证书的合法性的检查。
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//关闭直接输出
curl_setopt($ch,CURLOPT_POST,1);//使用post提交数据
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);//设置 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/537.36');//设置用户代理
curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);//设置头信息

curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file);//设置cookie的保存目录,这里很重要,你懂的(cookie你都不存,你以为你是麻花腾啊!)
$loginData = curl_exec($ch);//这里会返回token,需要处理一下。

//获取到token的值

$loginData = json_decode($loginData,true);

$token = explode("=",$loginData['ErrMsg']);

$token = array_pop($token);

echo "登录微信系统成功
";


curl_close($ch);


 

$password = md5($password);//Because the packet captured just now was found to be md5 encrypted, so here we encrypt the password in advance $post = "username={$username}&pwd={$password}&f=json&imgcode="; $loginUrl = "https://mp.weixin.qq.com/cgi-bin/login?";//WeChat login address //The header information here must be set, you can get these when capturing the packet just now $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 source of the authentication certificate, 0 means preventing the check of the validity of the certificate. 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);//Close direct output curl_setopt($ch,CURLOPT_POST,1);//Use post to submit data curl_setopt($ch,CURLOPT_POSTFIELDS,$post);//Set 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/537.36');//Set user agent curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);//Set header information curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file);//Set the cookie saving directory, this is very important, you know (you don’t even save cookies, you think you are Ma Huateng!) $loginData = curl_exec($ch); //Token will be returned here and needs to be processed. //Get the value of token $loginData = json_decode($loginData,true); $token = explode("=",$loginData['ErrMsg']); $token = array_pop($token); echo "Login to WeChat system successfully
"; curl_close($ch);

The above is the code to log in to the WeChat public platform. It has been tested and is very easy to use +_+.
In the past few days, I have been exposed to a lot of people on the WeChat public platform. This is just the first step in a long journey. Later I will share how to match WeChat’s fakeid and openid so that I can display the user’s complete information on my own platform (according to me Understand that there is currently no good solution on how to match fakeid and openid on the Internet. After several days of struggle, we can now match it. It is quite troublesome, and existing users cannot match it (in fact, this is theoretically possible) It's possible, but I haven't done it yet and don't talk nonsense. In fact, I don't have time to do this. If you have the opportunity, you can try it, but the implementation requires the support of the existing system, that is, your current system must record and use the chat history (I What did you say? I didn’t say anything!))).
Physical education teacher, you said that you didn’t take your physical education class well and didn’t come to teach us Chinese. I have encountered a lot of things that can be written these days, so I just thought of it and wrote it there. It’s a bit messy. In the past few days, The main one used is CURL, so today I will talk about some CURL examples, just to write down what I have on hand to log in to the WeChat public platform. CURL ends here, I may write more about the WeChat public platform later.
Attachment:

Alias ​​for

Options

Optionalvaluevalue

Remarks

CURLOPT_AUTOREFERER

Automatically sets header when redirecting based on Location: 🎜>Referer:information.

CURLOPT_BINARYTRANSFER

Returns native (Raw) output when CURLOPT_RETURNTRANSFER is enabled.

CURLOPT_COOKIESESSION

When enabledcurl will only pass one session cookie, ignoring other cookie, by default cURL will return all cookie to the server. session cookie refers to those cookies that exist to determine whether the server-side session is valid. .

CURLOPT_CRLF

When enabled, converts the line feed character of Unix into a carriage return and line feed character.

CURLOPT_DNS_USE_GLOBAL_CACHE

When enabled, a global DNS cache is enabled, which is thread-safe and enabled by default.

CURLOPT_FAILONERROR

displays the HTTP status code. The default behavior is to ignore whose number is less than or equal to 400 HTTPInformation.

CURLOPT_FILETIME

When enabled, an attempt will be made to modify information in the remote document. The result information will be returned through the CURLINFO_FILETIME option of the curl_getinfo() function. curl_getinfo().

CURLOPT_FOLLOWLOCATION

When enabled, the "Location: " returned by the server will be placed recursively in the header is returned to the server, use CURLOPT_MAXREDIRS to limit the number of recursive returns.

CURLOPT_FORBID_REUSE

Forcibly disconnect after completing the interaction and cannot be reused.

CURLOPT_FRESH_CONNECT

Force a new connection to replace the one in the cache.

CURLOPT_FTP_USE_EPRT

When enabled, when FTP downloads, use EPRT ( or LPRT) command. Disables EPRT and LPRT when set to FALSE , use the PORT commandonly.

CURLOPT_FTP_USE_EPSV

When enabled, try first PASV mode during FTP transfer >EPSV command. Disables the EPSV command when set to FALSE.

CURLOPT_FTPAPPEND

When enabled append writes to the file instead of overwriting it.

CURLOPT_FTPASCII

CURLOPT_TRANSFERTEXT.

CURLOPT_FTPLISTONLY

When enabled, only the names of the FTP directories are listed.

CURLOPT_HEADER

When enabled, the header file information will be output as a data stream.

CURLINFO_HEADER_OUT

The request string of the tracking handle when enabled.

Available starting with PHP 5.1.3 . CURLINFO_ The prefix is ​​intentional (intentional).

CURLOPT_HTTPGET

When enabled, the HTTP's method will be set to GET, because GET is the default, so it is only used when it is modified.

CURLOPT_HTTPPROXYTUNNEL

When enabled, it will be transmitted through the HTTP proxy.

CURLOPT_MUTE

When enabled, all modified parameters in the cURL function will be restored to their default values.

CURLOPT_NETRC

After the connection is established, access the ~/.netrc file to obtain the username and password information to connect to the remote site.

CURLOPT_NOBODY

When enabled, the BODY part in the HTML will not be output.

CURLOPT_NOPROGRESS

Close the progress bar of curl transfer when enabled. The default setting for this item is enabled.

Note:PHP automatically sets this option to TRUE, this option should only be changed for debugging purposes.

CURLOPT_NOSIGNAL

When enabled, ignores all curl signals passed to php. This is enabled by default during SAPI multi-thread transmission.

Added in cURL 7.10.

CURLOPT_POST

When enabled will send a regular POST request of type: application/x-www-form -urlencoded, just like form submission.

CURLOPT_PUT

Allows HTTP to send files when enabled, must set both CURLOPT_INFILE and CURLOPT_INFILESIZE.

CURLOPT_RETURNTRANSFER

Return the information obtained by curl_exec() in the form of a file stream instead of outputting it directly.

CURLOPT_SSL_VERIFYPEER

After disabling cURL will terminate validation from the server. Use the CURLOPT_CAINFO option to set the certificate Use the CURLOPT_CAPATH option to set the certificate directory if CURLOPT_SSL_VERIFYPEER(default is 2) is enabled, CURLOPT_SSL_VERIFYHOST needs to be Set to TRUE otherwise set to FALSE.

Defaults to TRUE since cURL 7.10. Default bundle installation starting with cURL 7.10.

CURLOPT_TRANSFERTEXT

When enabled uses ASCII mode for FTP transfers. For LDAP, it retrieves plain text information instead of HTML. On Windows systems, the system will not set STDOUT to binary mode.

CURLOPT_UNRESTRICTED_AUTH

Multiple locations in header generated using CURLOPT_FOLLOWLOCATION Continuously append username and password information in even if the domain name has changed.

CURLOPT_UPLOAD

Allow file uploads when enabled.

CURLOPT_VERBOSE

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1270
29
C# Tutorial
1250
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to solve the problem of JS resource caching in enterprise WeChat? How to solve the problem of JS resource caching in enterprise WeChat? Apr 04, 2025 pm 05:06 PM

Discussion on the JS resource caching issue of Enterprise WeChat. When upgrading project functions, some users often encounter situations where they fail to successfully upgrade, especially in the enterprise...

What is the difference between H5 page production and WeChat applets What is the difference between H5 page production and WeChat applets Apr 05, 2025 pm 11:51 PM

H5 is more flexible and customizable, but requires skilled technology; mini programs are quick to get started and easy to maintain, but are limited by the WeChat framework.

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

Ouyi Exchange app domestic download tutorial Ouyi Exchange app domestic download tutorial Mar 21, 2025 pm 05:42 PM

This article provides a detailed guide to safe download of Ouyi OKX App in China. Due to restrictions on domestic app stores, users are advised to download the App through the official website of Ouyi OKX, or use the QR code provided by the official website to scan and download. During the download process, be sure to verify the official website address, check the application permissions, perform a security scan after installation, and enable two-factor verification. During use, please abide by local laws and regulations, use a safe network environment, protect account security, be vigilant against fraud, and invest rationally. This article is for reference only and does not constitute investment advice. Digital asset transactions are at your own risk.

What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? Apr 01, 2025 pm 10:48 PM

Compatibility issues and troubleshooting methods for company security software and application. Many companies will install security software in order to ensure intranet security. However, security software sometimes...

What are the development tools for H5 and mini program? What are the development tools for H5 and mini program? Apr 06, 2025 am 09:54 AM

H5 development tools recommendations: VSCode, WebStorm, Atom, Brackets, Sublime Text; Mini Program Development Tools: WeChat Developer Tools, Alipay Mini Program Developer Tools, Baidu Smart Mini Program IDE, Toutiao Mini Program Developer Tools, Taro.

Detailed tutorial on how to buy and sell Binance virtual currency Detailed tutorial on how to buy and sell Binance virtual currency Mar 18, 2025 pm 01:36 PM

This article provides a brief guide to buying and selling of Binance virtual currency updated in 2025, and explains in detail the operation steps of virtual currency transactions on the Binance platform. The guide covers fiat currency purchase USDT, currency transaction purchase of other currencies (such as BTC), and selling operations, including market trading and limit trading. In addition, the guide also specifically reminds key risks such as payment security and network selection for fiat currency transactions, helping users to conduct Binance transactions safely and efficiently. Through this article, you can quickly master the skills of buying and selling virtual currencies on the Binance platform and reduce transaction risks.

See all articles