use WechatOfficialAccountMenuBundle\Service\MenuVersionService;
// Create version from current menu
$version = $menuVersionService->createVersion(
$account,
'2024 Spring Festival Menu'
);
// Create from existing version
$newVersion = $menuVersionService->createVersion(
$account,
'Adjusted from Spring Festival version',
$existingVersion
);
use WechatOfficialAccountMenuBundle\Service\WechatMenuApiService;
// Create personalized menu for specific user groups
$personalizedMenu = [
'button' => $menuButtons,
'matchrule' => [
'tag_id' => '101', // User tag ID
'sex' => '1', // Gender: 1=Male, 2=Female
'country' => 'CN', // Country
'province' => 'Guangdong', // Province
'city' => 'Guangzhou', // City
'client_platform_type' => '1', // Platform: 1=iOS, 2=Android, 3=Others
'language' => 'zh_CN' // Language
]
];
$wechatMenuApiService->addConditionalMenu($account, $personalizedMenu);
// Get menu click statistics (uClickStats($account, $startDate, $endDate);
// Monitor menu synchronization status
$syncStatus = $wechatMenuApiService->getCurrentMenuInfo($account);
if ($syncStatus['is_menu_open'] === 1) {
echo "Menu is enabled on WeChat";
}
use WechatOfficialAccountMenuBundle\Exception\WechatApiException;
try {
$menuVersionService->publishVersion($version);
} catch (WechatApiException $e) {
// Log the error
$logger->error('Menu publish failed', [
'error_code' => $e->getErrorCode(),
'error_msg' => $e->getMessage(),
'version_id' => $version->getId()
]);
// Attempt recovery
if ($e->getErrorCode() === 48001) { // API unauthorized
// Refresh access token and retry
$accessTokenService->refreshToken($account);
$menuVersionService->publishVersion($version);
}
}
// Always validate account ownership
if ($menu->getAccount()->getId() !== $currentUserAccountId) {
throw new AccessDeniedException('Insufficient permissions');
}
// Validate URLs before setting
if (!filter_var($url, FILTER_VALIDATE_URL)) {
throw new InvalidArgumentException('Invalid URL format');
}
use WechatOfficialAccountMenuBundle\Exception\MenuValidationException;
use WechatOfficialAccountMenuBundle\Exception\MenuVersionException;
use WechatOfficialAccountMenuBundle\Exception\WechatApiException;
try {
// Menu operations
} catch (MenuValidationException $e) {
// Menu validation failed
} catch (MenuVersionException $e) {
// Version operation failed
} catch (WechatApiException $e) {
// WeChat API call failed
$errorCode = $e->getErrorCode();
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.