尝试使用 arkesel 发送语音短信,但每当我运行命令时都会收到此错误 “拨打电话失败:Arkesel 错误:需要‘内容’密钥”
我尝试添加此
`->asMultipart()`
,因为使用带有“voice_file”(我上传到邮递员)和“recipients[]”(这是一个电话号码)的表单数据来测试邮递员的端点,它在邮递员中工作
public function makeCall($recipients)
{
$filePath = storage_path('app/public/assets/harvard.wav');
// Check if the file exists
if (!file_exists($filePath)) {
throw new Exception("Audio file not found at {$filePath}");
}
try {
$response = Http::withHeaders([
'api-key' => $this->apiKey
])
->attach('voice_file', fopen($filePath, 'r'), 'harvard.wav') // Attach the audio file
->post('https://sms.arkesel.com/api/v2/sms/voice/send', [
'recipients' => $recipients,
]);
// Check for successful response
if ($response->successful()) {
Log::info("Voice SMS sent successfully with Arkesel: ", ['recipients' => $recipients, 'response' => $response->json()]);
return $response->json();
} else {
Log::error("Voice SMS failed: ", ['recipients' => $recipients, 'error' => $response->body()]);
throw new Exception("Arkesel Error: " . $response->body());
}
} catch (Exception $e) {
Log::error("Voice SMS failed with exception: ", ['recipients' => $recipients, 'error' => $e->getMessage()]);
throw new Exception("Arkesel Error: " . $e->getMessage());
}
}```
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
public function makeCall($recipients)
{
$filePath = 'C:/Users/Dex/Downloads/native.mp3';
$apiKey = 'xxxxxxxxxxxxxxxxxxxx';
// Set up logging
$log = new Logger('voice_sms');
$log->pushHandler(new StreamHandler('sms.log', Logger::DEBUG));
// Check if the file exists
if (!file_exists($filePath)) {
throw new Exception("Audio file not found at {$filePath}");
}
// Set up Guzzle client
$client = new Client();
try {
$response = $client->post('https://sms.arkesel.com/api/v2/sms/voice/send', [
'headers' => [
'api-key' => $apiKey,
],
'multipart' => [
[
'name' => 'recipients[]',
'contents' => implode(',', $recipients) // Convert array to comma-separated string
],
[
'name' => 'voice_file',
'contents' => fopen($filePath, 'r'),
'filename' => 'native.mp3'
]
],
'http_errors' => false,
'timeout' => 0,
'allow_redirects' => [
'max' => 10,
'strict' => true,
'protocols' => ['http', 'https']
],
]);
// Check for successful response
if ($response->getStatusCode() === 200) {
$data = json_decode($response->getBody()->getContents(), true);
$log->info("Voice SMS sent successfully with Arkesel", ['recipients' => $recipients, 'response' => $data]);
return $data;
} else {
$log->error("Voice SMS failed", ['recipients' => $recipients, 'error' => $response->getBody()->getContents()]);
throw new Exception("Arkesel Error: " . $response->getBody()->getContents());
}
} catch (RequestException $e) {
$log->error("Voice SMS failed with exception", ['recipients' => $recipients, 'error' => $e->getMessage()]);
throw new Exception("Arkesel Error: " . $e->getMessage());
}
}