PHP ပေါင်းစပ်ခြင်း။
SMSBAT API ကို သင်၏ PHP အပလီကေးရှင်းတွင် ပေါင်းစပ်ခြင်းသည် တပ်ဆင်ထားသော cURL စာကြည့်တိုက် သို့မဟုတ် နာမည်ကြီး Guzzle HTTP client ကို အသုံးပြု၍ ရိုးရှင်းပါသည်။
Guzzle HTTP Client ကိုအသုံးပြုခြင်း (အကြံပြုထားသည်)
ပထမဦးစွာ Composer ကိုအသုံးပြု၍ Guzzle ကိုထည့်သွင်းပါ။
ထို့နောက် စာတိုပေးပို့ရန် အောက်ပါကုဒ်ကို အသုံးပြုပါ။
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
$client = new Client([
'base_uri' => 'https://api.smsbat.com',
'timeout' => 10.0,
]);
$apiKey = 'YOUR_API_KEY_HERE';
$payload = [
'messages' => [
[
'from' => 'ALPHANAME',
'to' => '380501234567',
'text' => 'Hello from PHP and SMSBAT!',
'type' => 'sms'
]
]
];
try {
$response = $client->post('/bat/messagelist', [
'headers' => [
'Content-Type' => 'application/json',
'X-Authorization-Key' => $apiKey
],
'json' => $payload
]);
echo "Status Code: " . $response->getStatusCode() . "\n";
echo "Response: " . $response->getBody() . "\n";
} catch (RequestException $e) {
echo "Error: " . $e->getMessage() . "\n";
if ($e->hasResponse()) {
echo "Response: " . $e->getResponse()->getBody() . "\n";
}
}
Native cURL ကို အသုံးပြုခြင်း။
အကယ်၍ သင်သည် ပြင်ပကုမ္ပဏီ ပက်ကေ့ဂျ်များကို အသုံးမပြုလိုပါက၊ သင်သည် PHP ၏ မူရင်း cURL တိုးချဲ့မှုကို အသုံးပြုနိုင်သည်။
<?php
$url = 'https://api.smsbat.com/bat/messagelist';
$apiKey = 'YOUR_API_KEY_HERE';
$payload = json_encode([
'messages' => [
[
'from' => 'ALPHANAME',
'to' => '380501234567',
'text' => 'Hello from PHP Native cURL!',
'type' => 'sms'
]
]
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-Authorization-Key: ' . $apiKey
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
echo 'cURL Error: ' . curl_error($ch);
} else {
echo "Status Code: $httpCode\n";
echo "Response: $response\n";
}
curl_close($ch);