|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once dirname(__DIR__) . '/vendor/autoload.php'; |
| 4 | + |
| 5 | +use GuzzleHttp\Client; |
| 6 | +use Nurigo\Solapi\Exceptions\MessageNotReceivedException; |
| 7 | +use Nurigo\Solapi\Models\Message; |
| 8 | +use Nurigo\Solapi\Services\SolapiMessageService; |
| 9 | + |
| 10 | +/** |
| 11 | + * 커스텀 PSR-18 HTTP 클라이언트를 이용한 SMS 발송 예제 |
| 12 | + * |
| 13 | + * SolapiMessageService 생성자의 3번째 인자로 PSR-18(Psr\Http\Client\ClientInterface) 호환 HTTP 클라이언트를 주입할 수 있습니다. |
| 14 | + * 이 예제에서는 Guzzle HTTP 클라이언트를 사용합니다. |
| 15 | + * |
| 16 | + * 사전 설치가 필요합니다: |
| 17 | + * composer require guzzlehttp/guzzle |
| 18 | + * |
| 19 | + * Guzzle 외에도 PSR-18을 구현한 다른 HTTP 클라이언트를 사용할 수 있습니다: |
| 20 | + * - Symfony HttpClient (symfony/http-client + symfony/psr-http-message-bridge) |
| 21 | + * - PHP-HTTP Curl Client (php-http/curl-client) |
| 22 | + */ |
| 23 | +try { |
| 24 | + // Guzzle 클라이언트 생성 (필요에 따라 옵션을 설정할 수 있습니다) |
| 25 | + $httpClient = new Client([ |
| 26 | + 'timeout' => 10, |
| 27 | + 'connect_timeout' => 5, |
| 28 | + // 'proxy' => 'http://proxy.example.com:8080', |
| 29 | + // 'verify' => '/path/to/cacert.pem', |
| 30 | + ]); |
| 31 | + |
| 32 | + // SolapiMessageService 생성 시 3번째 인자로 커스텀 HTTP 클라이언트를 전달합니다 |
| 33 | + $messageService = new SolapiMessageService("ENTER_YOUR_API_KEY", "ENTER_YOUR_API_SECRET", $httpClient); |
| 34 | + |
| 35 | + $message = new Message(); |
| 36 | + $message->setTo("수신번호") |
| 37 | + ->setFrom("계정에서 등록한 발신번호 입력") |
| 38 | + ->setText("한글 45자, 영자 90자 이하 입력되면 자동으로 SMS타입의 메시지가 발송됩니다."); |
| 39 | + |
| 40 | + $result = $messageService->send($message); |
| 41 | + print_r($result); |
| 42 | +} catch (MessageNotReceivedException $exception) { |
| 43 | + print_r($exception->getFailedMessageList()); |
| 44 | + print_r("----"); |
| 45 | + print_r($exception->getMessage()); |
| 46 | +} catch (Exception $exception) { |
| 47 | + print_r($exception->getMessage()); |
| 48 | +} |
0 commit comments