Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

php 分享微信好友

作者:死亡之神   发布日期:2025-12-02   浏览:204

要在PHP中实现分享到微信好友的功能,可以使用微信开放平台提供的接口。以下是一个简单的示例代码:

<?php
// 获取access_token
$appId = 'YOUR_APP_ID';
$appSecret = 'YOUR_APP_SECRET';
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret";

$response = file_get_contents($url);
$data = json_decode($response, true);
$accessToken = $data['access_token'];

// 获取ticket
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$accessToken&type=jsapi";

$response = file_get_contents($url);
$data = json_decode($response, true);
$ticket = $data['ticket'];

// 生成签名
$noncestr = 'YOUR_NONCESTR';
$timestamp = time();
$url = 'http://example.com'; // 当前页面的URL

$string = "jsapi_ticket=$ticket&noncestr=$noncestr&timestamp=$timestamp&url=$url";
$signature = sha1($string);

// 生成分享链接
$shareUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appId&redirect_uri=$url&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";

// 输出分享链接
echo '<a href="' . $shareUrl . '">分享到微信好友</a>';
?>

请注意替换示例代码中的YOUR_APP_IDYOUR_APP_SECRETYOUR_NONCESTR为你自己的实际值。此外,http://example.com也需要替换为你的网站的实际URL。

这段代码首先通过App ID和App Secret获取access_token,然后通过access_token获取ticket。接下来,根据生成签名所需的参数生成签名。最后,使用App ID和生成的签名生成分享链接,将其输出到页面上的链接中。用户点击该链接后将跳转到微信授权页面,进行登录和授权操作。

请注意,此示例仅演示了如何生成分享链接,实际的分享行为需要在微信授权后的页面中进行处理。

上一篇:php发邮件类 附件

下一篇:PHP去除头部

大家都在看

php session用法

php 定义常量

phpisset函数

php html转图片

php后端

php爬虫框架

php读取csv文件

php+mysql动态网站开发

phpmyadmin docker

php session id

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站