Home php教程 php手册 使用PHP调用微信API,使用微信做通知类应用的方法

使用PHP调用微信API,使用微信做通知类应用的方法

Jun 06, 2016 pm 07:47 PM
api php w use WeChat method use transfer notify

相比于最常用的短信和邮件的通知方式,微信有着无可比拟的优势:快速、免费,特别适合用在报警通知类应用上。 但是微信并没有提供现成的api接口来给好友发送信息,公众平台也只能被动回复。那么如何才能主动发信息呢?答案就在微信网页版。 仔细观察微信网页

相比于最常用的短信和邮件的通知方式,微信有着无可比拟的优势:快速、免费,特别适合用在报警通知类应用上。

但是微信并没有提供现成的api接口来给好友发送信息,公众平台也只能被动回复。那么如何才能主动发信息呢?答案就在微信网页版。

仔细观察微信网页版和服务器请求的记录,发现完全就是以oauth授权方式运行,完全不用考虑cookie。据此,分析请求记录,就能模拟网页版登录微信,从而实现主动向用户发送消息的功能。

以下是用到的请求:

1. 获取uuid,从返回的数据中,找到uuid

https://login.weixin.qq.com/jslogin?appid=wx782c26e4c19acffb&redirect_uri=https%3A%2F%2Fwx.qq.com%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxnewloginpage&fun=new&lang=zh_CN

2. 获取二维码图片,然后使用微信扫描

https://login.weixin.qq.com/qrcode/$this->uuid?t=webwx

3. 获取令牌,轮询,直到从返回的数据中找到window.redirect_uri=xxxx的代码,xxx就是令牌url

https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?uuid=$this->uuid&tip=1

4. 访问令牌url,获取sid。从返回的头信息中,分析出Uin,sid和uuid,保存起来

xxxxxx

5. 初始化微信,从返回的数据中读取UserName

1

2

3

4

5

6

7

8

9

$data = array(

    "BaseRequest"=>array(

        "DeviceID"  => 'e538770852445779',

        "Sid"   => $this->sid,

        "Skey"  => "",

        "Uin"   => $this->Uin,

    )

);

$this->post_contents('https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit', $this->encode($data));

6. 获取Skey

1

$this->post_contents('https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid=' . urlencode($this->sid), '{"BaseRequest":{"Uin":'.$this->Uin.',"Sid":"'.$this->sid.'"},"SyncKey":{"Count":0,"List":[]}}');

7. 发送消息,POST方式,

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

$data = array(

    "BaseRequest" => array(

        "Uin"=> $this->Uin,

        "Sid"=> $this->sid,

        "Skey"=> $this->Skey,

        "DeviceID"=> $this->DeviceID,

    ),

    "Msg" => array(

        "FromUserName" => $this->FromUserName,

        "ToUserName" => $name,

        "Type" => 1,

        "Content" => $content,

        "ClientMsgId" => $this->ClientMsgId,

        "LocalID" => $this->ClientMsgId,

    ),

);

$str = $this->encode($data);

son_decode($this->post_contents('https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg?sid=' . urlencode($this->sid) . '&r=' .$this->ClientMsgId, $str), true);

发送消息的经典过程为:

  1. 初始化对象,设置Uin,sid,uuid
  2. 更新Skey
  3. 发送信息

但是微信网页版长时间不登录会失效,所以还需要写个任务去ping

1

2

$data = '{"BaseRequest":{"Uin":'.$this->Uin.',"Sid":"'.$this->sid.'"},"SyncKey":{"Count":0,"List":[]}}';

        $ret = json_decode($this->post_contents('https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid=' . urlencode($this->sid), $data, 30), true);

微信的数据都是以json格式传输的,但是他这个json比较特殊,还需要用特别的函数来实现

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

protected function encode($data){

    if(!is_array($data)){

        return $this->encode_str($data);

    }

    $ds = array();

    foreach($data as $k => $v){

        $ds [] = "\"$k\":" . $this->encode($v);

    }

    return '{' . join(',', $ds) . '}';

}

 

protected function encode_str($str){

    if(preg_match('|^\d+$|', $str)){

        return $str;

    }

    return '"' . str_replace('"', '\"', i

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 and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 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 in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 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.

See all articles