1. Go to this page and download the library: Download tourze/jiguang-sms-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use JiguangSmsBundle\Service\JiguangSmsService;
use JiguangSmsBundle\Request\Message\SendMessageRequest;
use JiguangSmsBundle\Entity\Account;
use JiguangSmsBundle\Repository\AccountRepository;
// Inject the service
public function __construct(
private JiguangSmsService $jiguangSmsService,
private AccountRepository $accountRepository
) {}
public function sendSms(): void
{
$account = $this->accountRepository->findOneBy(['appKey' => 'your_app_key']);
$request = new SendMessageRequest();
$request->setAccount($account)
->setMobile('13800138000')
->setTempId('your_template_id')
->setTempParas(['code' => '123456']);
$response = $this->jiguangSmsService->request($request);
// Response contains msg_id for tracking
$msgId = $response['msg_id'];
}
use JiguangSmsBundle\Request\Code\SendTextCodeRequest;
use JiguangSmsBundle\Request\Code\SendVoiceCodeRequest;
use JiguangSmsBundle\Request\Code\VerifyCodeRequest;
// Send text verification code
$request = new SendTextCodeRequest();
$request->setAccount($account)
->setMobile('13800138000')
->setTempId('your_template_id');
$response = $this->jiguangSmsService->request($request);
// Send voice verification code
$voiceRequest = new SendVoiceCodeRequest();
$voiceRequest->setAccount($account)
->setMobile('13800138000')
->setTtl(300); // 5 minutes
$voiceResponse = $this->jiguangSmsService->request($voiceRequest);
// Verify code
$verifyRequest = new VerifyCodeRequest();
$verifyRequest->setAccount($account)
->setMsgId($response['msg_id'])
->setCode('user_input_code');
$verifyResponse = $this->jiguangSmsService->request($verifyRequest);
$isValid = $verifyResponse['is_valid'] ?? false;
use JiguangSmsBundle\Service\TemplateService;
use JiguangSmsBundle\Service\SignService;
use JiguangSmsBundle\Entity\Template;
use JiguangSmsBundle\Entity\Sign;
class SmsManagementService
{
public function __construct(
private TemplateService $templateService,
private SignService $signService
) {}
public function createTemplate(Template $template): void
{
// Create template remotely and sync local ID
$this->templateService->createRemoteTemplate($template);
// Sync status periodically
$this->templateService->syncTemplateStatus($template);
}
public function createSignature(Sign $sign): void
{
// Create signature remotely and sync local ID
$this->signService->createRemoteSign($sign);
// Sync status periodically
$this->signService->syncSignStatus($sign);
}
}
use JiguangSmsBundle\Request\Message\SendMessageRequest;
use JiguangSmsBundle\Service\JiguangSmsService;
use JiguangSmsBundle\Entity\Account;
class CustomSmsHandler
{
public function __construct(private JiguangSmsService $smsService) {}
public function sendBulkMessages(Account $account, array $recipients, string $templateId, array $params = []): array
{
$results = [];
foreach ($recipients as $mobile) {
$request = new SendMessageRequest();
$request->setAccount($account)
->setMobile($mobile)
->setTempId($templateId)
->setTempParas($params);
$results[] = $this->smsService->request($request);
}
return $results;
}
}
use JiguangSmsBundle\Exception\JiguangSmsException;
use JiguangSmsBundle\Exception\InvalidSignStatusException;
use JiguangSmsBundle\Exception\InvalidTemplateStatusException;
try {
$this->jiguangSmsService->request($request);
} catch (InvalidSignStatusException $e) {
// Handle invalid signature status
} catch (InvalidTemplateStatusException $e) {
// Handle invalid template status
} catch (JiguangSmsException $e) {
// Handle general Jiguang SMS errors
}
use Symfony\Component\RateLimiter\RateLimiterFactory;
class SmsController
{
public function __construct(
private RateLimiterFactory $smsLimiter,
private JiguangSmsService $smsService
) {}
public function sendVerificationCode(Request $request): Response
{
$limiter = $this->smsLimiter->create($request->getClientIp());
if (!$limiter->consume(1)->isAccepted()) {
throw new TooManyRequestsHttpException();
}
// Send SMS...
}
}
use Symfony\Component\Validator\Constraints as Assert;
class SmsRequest
{
#[Assert\NotBlank]
#[Assert\Regex(pattern: '/^1[3-9]\d{9}$/', message: 'Invalid mobile number')]
private string $mobile;
#[Assert\NotBlank]
#[Assert\Length(max: 500)]
private string $content;
}
// Make sure your account exists in the database
$account = $this->accountRepository->findOneBy(['appKey' => 'your_app_key']);
if (!$account) {
throw new \Exception('Account not found');
}
// Check your app_key and master_secret are correct
// Verify the account is marked as valid
$account->setValid(true);
$this->entityManager->flush();
bash
# Check if cron jobs are registered
php bin/console cron:list
# Manually run synchronization commands
php bin/console jiguang:sms:sync-account-balance
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.