1. Go to this page and download the library: Download edenohana/sms-free-php 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/ */
edenohana / sms-free-php example snippets
use EdenOhana\SmsFree\Credentials;
use EdenOhana\SmsFree\Sms4FreeClient;
$client = new Sms4FreeClient(new Credentials('username', 'password', 'api-key'));
$result = $client->send(
senderName: 'MyShop', // a verified sender number, or an approved sender ID
recipients: ['054-123-4567'], // one number or a list
message: 'ההזמנה שלך יצאה לדרך',
);
echo $result->acceptedCount(); // 1
$client = new Sms4FreeClient(Credentials::fromEnvironment());
// reads SMS4FREE_USERNAME, SMS4FREE_PASSWORD and SMS4FREE_API_KEY
use EdenOhana\SmsFree\Exception\ApiException;
use EdenOhana\SmsFree\Exception\InvalidPhoneNumberException;
use EdenOhana\SmsFree\Exception\SmsFreeException;
use EdenOhana\SmsFree\Exception\TransportException;
try {
$client->send('MyShop', $recipients, $text);
} catch (InvalidPhoneNumberException $e) {
// Bad user input. Nothing was sent, nothing was charged.
$form->addError('phone', implode(', ', $e->invalidNumbers()));
} catch (ApiException $e) {
// The provider refused: wrong credentials, no balance, unverified sender.
$logger->error('SMS4Free refused', ['status' => $e->status(), 'reason' => $e->providerMessage()]);
} catch (TransportException $e) {
// Network trouble. The message may or may not have gone out, see the note on retries below.
$logger->warning('SMS4Free unreachable', ['error' => $e->getMessage()]);
} catch (SmsFreeException $e) {
// Anything else from this library.
}
$invalid = $client->findInvalidRecipients($rowsFromCsv);
if ($invalid !== []) {
throw new RuntimeException('Unusable numbers: ' . implode(', ', $invalid));
}
$result = $client->sendBulk('MyShop', $rowsFromCsv, $text);
echo $result->inputCount(); // every input row
echo $result->submittedCount(); // valid unique numbers handed to SMS4Free
echo $result->acceptedCount(); // count reported by SMS4Free
foreach ($result->skippedRecipients() as $row) {
printf("Row %d is invalid: %s\n", $row->rowNumber(), $row->rawValue());
}
foreach ($result->duplicateRecipients() as $row) {
printf("Row %d duplicates row %d\n", $row->rowNumber(), $row->originalRowNumber());
}
$credits = $client->balance(); // zero is a valid empty balance
$result = $client->send('MyShop', $rowsFromCsv, $text);
if ($result->hasSkippedRecipients()) {
$logger->warning('Left out of the send', ['numbers' => $result->skippedRecipients()]);
}
use EdenOhana\SmsFree\ClientOptions;
use EdenOhana\SmsFree\InvalidRecipientPolicy;
$client = new Sms4FreeClient(
Credentials::fromEnvironment(),
(new ClientOptions())->withInvalidRecipientPolicy(InvalidRecipientPolicy::RejectRequest),
);
use EdenOhana\SmsFree\PhoneNumber;
$number = PhoneNumber::parse('054-123-4567');
$number->national(); // '0541234567', what the provider is given
$number->e164(); // '+972541234567', what you want in your database
$number->raw(); // '054-123-4567', what the user typed
use EdenOhana\SmsFree\Message;
$message = Message::of('הקוד שלך לאימות הוא 123456');
$message->encoding(); // SmsEncoding::Ucs2
$message->length(); // 26 characters
$message->parts(); // 1, network SMS parts
$message->estimatedCreditsPerRecipient(); // 1, SMS4Free billing estimate
$options = (new ClientOptions())->withMaxMessageLength(500);
use EdenOhana\SmsFree\ClientOptions;
$client = new Sms4FreeClient(
Credentials::fromEnvironment(),
(new ClientOptions())->withMessageTruncation(true),
);
use EdenOhana\SmsFree\Otp\OtpGenerator;
$code = (new OtpGenerator(length: 6))->generate(); // '042317', a string, so leading zeros survive
$client->send('MyShop', [$phone], "הקוד שלך לאימות הוא: {$code}");
// Later, when the user types it back:
OtpGenerator::matches($storedCode, $typedCode); // constant-time comparison
use EdenOhana\SmsFree\ClientOptions;
$options = (new ClientOptions())
->withTimeouts(connectTimeout: 3.0, timeout: 10.0)
->withInternationalRecipients(true) // accept non-Israeli numbers
->withInvalidRecipientPolicy(InvalidRecipientPolicy::SkipInvalid)
->withMaxMessageLength(500) // optional application cap
->withBalanceEndpoint('https://example.test/balance')
->withUserAgent('my-app/3.0')
->withCaBundlePath('/etc/ssl/certs/cacert.pem'); // for hosts with no CA store
$client = new Sms4FreeClient(Credentials::fromEnvironment(), $options);
final class GuzzleTransport implements HttpClient
{
public function post(string $url, string $body, array $headers = []): HttpResponse
{
// ...
}
}
$client = new Sms4FreeClient($credentials, new ClientOptions(), new GuzzleTransport());
// Notification
public function via(object $notifiable): array
{
return ['sms4free'];
}
public function toSms4Free(object $notifiable): string
{
return "הקוד שלך לאימות הוא: {$this->code}";
}
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.