Update several mail and SMS drivers to use HTTP class instead of Requests or raw curl

This commit is contained in:
Kijin Sung 2023-04-23 00:40:30 +09:00
parent cf84d70ca1
commit 4f73c9562b
4 changed files with 25 additions and 41 deletions

View file

@ -92,6 +92,7 @@ class Mailgun extends Base implements \Rhymix\Framework\Drivers\MailInterface
$boundary = str_repeat('-', 24) . substr(md5(mt_rand()), 0, 16); $boundary = str_repeat('-', 24) . substr(md5(mt_rand()), 0, 16);
$headers = array( $headers = array(
'Content-Type' => 'multipart/form-data; boundary=' . $boundary, 'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
'User-Agent' => 'PHP',
); );
$data = implode("\r\n", array( $data = implode("\r\n", array(
'--' . $boundary, '--' . $boundary,
@ -107,16 +108,15 @@ class Mailgun extends Base implements \Rhymix\Framework\Drivers\MailInterface
'--' . $boundary . '--', '--' . $boundary . '--',
'', '',
)); ));
$options = array( $settings = array(
'auth' => array('api', $this->_config['api_token']), 'auth' => array('api', $this->_config['api_token']),
'timeout' => 5, 'timeout' => 5,
'useragent' => 'PHP',
); );
// Send the API request. // Send the API request.
$url = self::$_url . '/' . $this->_config['api_domain'] . '/messages.mime'; $url = self::$_url . '/' . $this->_config['api_domain'] . '/messages.mime';
$request = \Requests::post($url, $headers, $data, $options); $request = \Rhymix\Framework\HTTP::post($url, $data, $headers, [], $settings);
$result = @json_decode($request->body); $result = @json_decode($request->getBody()->getContents());
// Parse the result. // Parse the result.
if (!$result) if (!$result)

View file

@ -147,25 +147,26 @@ class SendGrid extends Base implements \Rhymix\Framework\Drivers\MailInterface
$headers = array( $headers = array(
'Authorization' => 'Bearer ' . $this->_config['api_token'], 'Authorization' => 'Bearer ' . $this->_config['api_token'],
'Content-Type' => 'application/json', 'Content-Type' => 'application/json',
'User-Agent' => 'PHP',
); );
$options = array( $options = array(
'timeout' => 8, 'timeout' => 8,
'useragent' => 'PHP',
); );
// Send the API request. // Send the API request.
$request = \Requests::post(self::$_url, $headers, json_encode($data), $options); $request = \Rhymix\Framework\HTTP::post(self::$_url, $data, $headers, [], $options);
$response_code = intval($request->status_code);; $status_code = $request->getStatusCode();
$result = $request->getBody()->getContents();
// Parse the result. // Parse the result.
if (!$response_code) if (!$status_code)
{ {
$message->errors[] = 'SendGrid: Connection error: ' . $request->body; $message->errors[] = 'SendGrid: Connection error: ' . $result;
return false; return false;
} }
elseif ($response_code > 202) elseif ($status_code > 202)
{ {
$message->errors[] = 'SendGrid: Response code ' . $response_code . ': ' . $request->body; $message->errors[] = 'SendGrid: Response code ' . $status_code . ': ' . $result;
return false; return false;
} }

View file

@ -11,6 +11,7 @@ class Woorimail extends Base implements \Rhymix\Framework\Drivers\MailInterface
* The API URL. * The API URL.
*/ */
protected static $_url = 'https://woorimail.com/index.php'; protected static $_url = 'https://woorimail.com/index.php';
protected static $_timeout = 5;
/** /**
* Error codes and messages. * Error codes and messages.
@ -171,16 +172,12 @@ class Woorimail extends Base implements \Rhymix\Framework\Drivers\MailInterface
$headers = array( $headers = array(
'Accept' => 'application/json, text/javascript, */*; q=0.1', 'Accept' => 'application/json, text/javascript, */*; q=0.1',
); );
$options = array(
'timeout' => 5,
'useragent' => 'PHP',
);
// Send the API request. // Send the API request.
try try
{ {
$request = \Requests::post(self::$_url, $headers, $data, $options); $request = \Rhymix\Framework\HTTP::post(self::$_url, $data, $headers, [], ['timeout' => self::$_timeout]);
$result = @json_decode($request->body); $result = @json_decode($request->getBody()->getContents());
} }
catch (\Requests_Exception $e) catch (\Requests_Exception $e)
{ {

View file

@ -11,6 +11,7 @@ class iwinv extends Base implements \Rhymix\Framework\Drivers\SMSInterface
* API endpoint URL (fallback if URL is not explicitly configured) * API endpoint URL (fallback if URL is not explicitly configured)
*/ */
const LEGACY_API_URL = 'https://sms.service.iwinv.kr/send/'; const LEGACY_API_URL = 'https://sms.service.iwinv.kr/send/';
const DEFAULT_TIMEOUT = 5;
/** /**
* API specifications. * API specifications.
@ -80,14 +81,13 @@ class iwinv extends Base implements \Rhymix\Framework\Drivers\SMSInterface
public function send(array $messages, \Rhymix\Framework\SMS $original) public function send(array $messages, \Rhymix\Framework\SMS $original)
{ {
$status = true; $status = true;
$curl = null;
foreach ($messages as $i => $message) foreach ($messages as $i => $message)
{ {
// Authentication // Authentication
$headers = array( $headers = array(
'Content-Type: multipart/form-data', 'Content-Type' => 'multipart/form-data',
'secret: ' . base64_encode($this->_config['api_key'] . '&' . $this->_config['api_secret']), 'secret' => base64_encode($this->_config['api_key'] . '&' . $this->_config['api_secret']),
); );
// Sender and recipient // Sender and recipient
@ -109,7 +109,7 @@ class iwinv extends Base implements \Rhymix\Framework\Drivers\SMSInterface
$data['text'] = $message->content; $data['text'] = $message->content;
// Image attachment // Image attachment
if ($message->image) if (!empty($message->image))
{ {
$data['image'] = curl_file_create(realpath($message->image)); $data['image'] = curl_file_create(realpath($message->image));
} }
@ -130,24 +130,15 @@ class iwinv extends Base implements \Rhymix\Framework\Drivers\SMSInterface
$api_url = self::LEGACY_API_URL; $api_url = self::LEGACY_API_URL;
} }
// We need to use curl because Filehandler::getRemoteResource() doesn't work with this API for some reason. // Send the request.
if (!$curl) $request = \Rhymix\Framework\HTTP::post($api_url, $data, $headers, [], ['timeout' => self::DEFAULT_TIMEOUT]);
{ $result = $request->getBody()->getContents();
$curl = curl_init(); $status_code = $request->getStatusCode();
}
curl_setopt($curl, \CURLOPT_URL, $api_url);
curl_setopt($curl, \CURLOPT_TIMEOUT, 5);
curl_setopt($curl, \CURLOPT_POST, 1);
curl_setopt($curl, \CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, \CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, \CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($curl);
$err = curl_error($curl);
// Check the result. // Check the result.
if ($err) if ($status_code !== 200)
{ {
$original->addError('API error while sending message ' . ($i + 1) . ' of ' . count($messages) . ': ' . $err); $original->addError('API error while sending message ' . ($i + 1) . ' of ' . count($messages) . ': ' . $status_code);
$status = false; $status = false;
} }
elseif (trim($result) === '') elseif (trim($result) === '')
@ -162,11 +153,6 @@ class iwinv extends Base implements \Rhymix\Framework\Drivers\SMSInterface
} }
} }
if ($curl)
{
@curl_close($curl);
}
return $status; return $status;
} }
} }