tourze/yunpian-sms-bundle

云片短信

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

0.0.1 2025-05-25 17:20 UTC

This package is auto-updated.

Last update: 2025-06-11 22:09:49 UTC


README

English | 中文

Latest Version Total Downloads License

A Symfony bundle that integrates with Yunpian SMS Service API for sending and managing SMS messages.

Features

  • Send domestic SMS messages
  • Manage SMS templates
  • Query sending records
  • Check account balance
  • Sign management
  • Daily consumption statistics
  • Full integration with Symfony framework

Requirements

  • PHP >= 8.1
  • Symfony Framework >= 6.4
  • Doctrine ORM

Installation

Step 1: Add the dependency with Composer

composer require tourze/yunpian-sms-bundle

Step 2: Register the bundle in config/bundles.php

return [
    // ...
    YunpianSmsBundle\YunpianSmsBundle::class => ['all' => true],
];

Configuration

This bundle requires you to create at least one account in the database. The Account entity stores the API key required for authentication with Yunpian's services.

You can create an account by inserting a record into the ims_yunpian_account table or use the admin interface if available:

use YunpianSmsBundle\Entity\Account;

// Create a new account
$account = new Account();
$account->setApiKey('your_yunpian_api_key');
$account->setValid(true);
$account->setRemark('Main Account');

// Persist the account
$entityManager->persist($account);
$entityManager->flush();

Quick Start

1. Send SMS

use YunpianSmsBundle\Service\SendLogService;
use YunpianSmsBundle\Repository\AccountRepository;

class YourService
{
    public function __construct(
        private readonly SendLogService $sendLogService,
        private readonly AccountRepository $accountRepository,
    ) {
    }

    public function sendMessage(): void
    {
        $account = $this->accountRepository->findOneBy(['valid' => true]);

        $this->sendLogService->send(
            account: $account,
            mobile: '13800138000',
            content: 'Your verification code is 1234'
        );
    }
}

2. Send SMS using template

use YunpianSmsBundle\Repository\TemplateRepository;

// In your service method
$account = $this->accountRepository->findOneBy(['valid' => true]);
$template = $this->templateRepository->findOneBy(['tplId' => 'your_template_id']);

$this->sendLogService->sendTpl(
    account: $account,
    template: $template,
    mobile: '13800138000',
    tplValue: ['code' => '1234']
);

3. Query send records

// Set up request parameters
$request = new GetSendRecordRequest();
$request->setAccount($account);
$request->setStartTime(new \DateTime('-7 days'));
$request->setEndTime(new \DateTime());

// Get the records
$response = $this->apiClient->request($request);

4. Manage templates

// Sync templates from Yunpian to local database
$this->templateService->syncTemplates($account);

// Get all templates
$templates = $this->templateRepository->findBy(['account' => $account]);

API Documentation

For detailed API documentation, please refer to the Yunpian SMS Official Documentation

Contributing

Please see CONTRIBUTING.md for details.

License

This bundle is released under the MIT License. See the LICENSE file for more details.