Home Backend Development PHP Tutorial php WeChat public account js-sdk development application

php WeChat public account js-sdk development application

May 30, 2018 am 11:31 AM
javascript js-sdk php

This article mainly introduces the relevant information of PHP WeChat official account js-sdk development and application in detail. It has certain reference value. Interested friends can refer to

test js interface. Function, I am using BAE server not SAE server. The SAE server cannot write files and there will be problems with the test.
What is js-sdk, let me first demonstrate the specific function of the js-sdk interface.

I randomly open a push article on WeChat as shown below:

php WeChat public account js-sdk development application

Click on the first article


The above is our connection article. If we send this article to a friend, the display effect will be as follows:

php WeChat public account js-sdk development application

Share to Moments and let’s see the effect:

php WeChat public account js-sdk development application

Why is it also an article link, in the official account, sent to friends, The interface effect displayed when sending to Moments is different. The display effect of the official account is because the WeChat official account calls the WeChat message interface when pushing, and the latter two are because the page calls the SDK interface related to js-sdk.
Send to friends calls the js interface to obtain the click status of the "Share to friends" button and the custom sharing content interface.
Send to Moments calls the js interface to obtain the click status of the "Share to Moments" button and the custom sharing content interface.

Let’s implement the steps to call the js-sdk interface:

Step one: Enter the "Function Settings" of the "Public Account Settings" on the WeChat public platform and fill in the "JS interface security domain name".

As shown below:

php WeChat public account js-sdk development application

Only pages placed under this domain name can call the js interface.

Second step: http://demo.open.weixin.qq.com/jssdk/sample.zip Connect to this address to download, open the php folder inside, You found 4 php files

php WeChat public account js-sdk development application

The first 3 php files are configuration files, don’t touch them, sample.php is what we opened in WeChat Connection page, in this page we write our page code. I used this file for testing, and the code inside is different from the original version.

<?php
// echo "nihao";
require_once "jssdk.php";
$jssdk = new JSSDK("wxaf471be5d2bc9521", "93f5c3817074d43e7a7ab3f403cc72dd");//JSSDK中的参数是appid和appsecret。
$signPackage = $jssdk->GetSignPackage();//获取配置参数。
?>
Copy after login

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
</head>
<body>
 <p>我靠</p>
 <p>猪八戒</p>
 <button style="color: red" id="btn">你好</button>
 <button id="btn1">唐生</button>
</body>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>//这个是js-sdk文件,一定要引入。
<script>

 var button = document.getElementsByTagName("button");
 var btn = document.getElementById("btn");
 var p = document.getElementsByTagName("p")[0];
 var btn1 = document.getElementById("btn1");
 //wx表示一个js-sdk构建的一个对象。config()是它的一个方法,这个接口用来验证配置。
 wx.config({
 debug: true,//true表示每调用一个接口信息都会在页面alert一段字符串。方便测试过程中国是否能正确调用接口。
 appId: &#39;<?php echo $signPackage["appId"];?>&#39;,
 timestamp: <?php echo $signPackage["timestamp"];?>,
 nonceStr: &#39;<?php echo $signPackage["nonceStr"];?>&#39;,
 signature: &#39;<?php echo $signPackage["signature"];?>&#39;,
 //上面的4个参数,如果你像深入了解其来源就去查看jssdk.php代码。测试过程不需要对上面的代码做任何操作。
 jsApiList: [
  // 所有要调用的 API 都要加到这个列表中
  &#39;onMenuShareAppMessage&#39;,//获取“分享给朋友”按钮点击状态及自定义分享内容接口。
  &#39;chooseImage&#39;,//获取手机相片接口
  &#39;openLocation&#39;//获取微信地图接口
 ]
 });

//所有的js调用接口都要放到下面的ready()中。
 wx.ready(function () {
 // 在这里调用 API
 //获取“分享给朋友”按钮点击状态,点击后发送给朋友微信,微信上的推送显示内容就是下面的数据
  wx.onMenuShareAppMessage({
   title: &#39;大圣&#39;, // 分享标题
   desc: &#39;我要坚持&#39;, // 分享描述
   link: &#39;http://www.baidu.com&#39;, // 分享链接
   imgUrl: &#39;http://discuz.comli.com/weixin/weather/icon/cartoon.jpg&#39;, // 分享图标
   type: &#39;link&#39;, // 分享类型,music、video或link,不填默认为link
   dataUrl: &#39;&#39;, // 如果type是music或video,则要提供数据链接,默认为空
   success: function () { 
    // 用户确认分享后执行的回调函数
    alert("success");
   },
   cancel: function () { 
    // 用户取消分享后执行的回调函数
    alert("error");
   }
  });
 //拍照或从手机相册选择。点击页面上的按钮就可以调用手机里的相册。
  btn.onclick = function(){
  wx.chooseImage({
   count: 1, // 默认9
   sizeType: [&#39;original&#39;, &#39;compressed&#39;], // 可以指定是原图还是压缩图,默认二者都有
   sourceType: [&#39;album&#39;, &#39;camera&#39;], // 可以指定来源是相册还是相机,默认二者都有
   success: function (res) {
    var localIds = res.localIds; // 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片
   }
  })
  };
 //微信内置地图接口。点击该按钮就可转到显示当前位置的地图页面
  btn1.onclick = function() {

  wx.openLocation({
 latitude: 0, // 纬度,浮点数,范围为90 ~ -90
 longitude: 0, // 经度,浮点数,范围为180 ~ -180。
 name: &#39;&#39;, // 位置名
 address: &#39;&#39;, // 地址详情说明
 scale: 1, // 地图缩放级别,整形值,范围从1~28。默认为最大
 infoUrl: &#39;&#39; // 在查看位置界面底部显示的超链接,可点击跳转
});
  }

 });
// wx.checkJsApi({
//  jsApiList: [&#39;onMenuShareAppMessage&#39;], // 需要检测的JS接口列表,所有JS接口列表见附录2,
//  success: function(res) {
//   // 以键值对的形式返回,可用的api值true,不可用为false
//   // 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
//   alert("success");
//  }
// });

// btn.onclick = function() {
//  p.innerText = "齐天大圣";
//  wx.onMenuShareAppMessage({
//  title: &#39;大圣&#39;, // 分享标题
//  desc: &#39;我要坚持&#39;, // 分享描述
//  link: &#39;http://www.baidu.com&#39;, // 分享链接
//  imgUrl: &#39;http://discuz.comli.com/weixin/weather/icon/cartoon.jpg&#39;, // 分享图标
//  type: &#39;link&#39;, // 分享类型,music、video或link,不填默认为link
//  dataUrl: &#39;&#39;, // 如果type是music或video,则要提供数据链接,默认为空
//  success: function () { 
//   // 用户确认分享后执行的回调函数
//   alert("success");
//  },
//  cancel: function () { 
//   // 用户取消分享后执行的回调函数
//   alert("error");
//  }
// });
 // }
</script>
</html>
Copy after login

To test the interface function of js, I am using the BAE server, not the SAE server. The SAE server cannot write files and there will be problems with the test.
For more functions and detailed information, please visit http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html

The above is the entire content of this article, I hope it will help everyone learn Helps.


Related recommendations:

Summary of PHP knowledge about objects

## The difference between constants, static properties, and non-static properties in php

Classes and objects in php: Detailed graphic explanation of static properties and static methods

The above is the detailed content of php WeChat public account js-sdk development application. For more information, please follow other related articles on the PHP Chinese website!

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1246
24
Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

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 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.

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

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles