mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-06 10:11:38 +09:00
Update several mail and SMS drivers to use HTTP class instead of Requests or raw curl
This commit is contained in:
parent
cf84d70ca1
commit
4f73c9562b
4 changed files with 25 additions and 41 deletions
|
|
@ -92,6 +92,7 @@ class Mailgun extends Base implements \Rhymix\Framework\Drivers\MailInterface
|
|||
$boundary = str_repeat('-', 24) . substr(md5(mt_rand()), 0, 16);
|
||||
$headers = array(
|
||||
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
|
||||
'User-Agent' => 'PHP',
|
||||
);
|
||||
$data = implode("\r\n", array(
|
||||
'--' . $boundary,
|
||||
|
|
@ -107,16 +108,15 @@ class Mailgun extends Base implements \Rhymix\Framework\Drivers\MailInterface
|
|||
'--' . $boundary . '--',
|
||||
'',
|
||||
));
|
||||
$options = array(
|
||||
$settings = array(
|
||||
'auth' => array('api', $this->_config['api_token']),
|
||||
'timeout' => 5,
|
||||
'useragent' => 'PHP',
|
||||
);
|
||||
|
||||
// Send the API request.
|
||||
$url = self::$_url . '/' . $this->_config['api_domain'] . '/messages.mime';
|
||||
$request = \Requests::post($url, $headers, $data, $options);
|
||||
$result = @json_decode($request->body);
|
||||
$request = \Rhymix\Framework\HTTP::post($url, $data, $headers, [], $settings);
|
||||
$result = @json_decode($request->getBody()->getContents());
|
||||
|
||||
// Parse the result.
|
||||
if (!$result)
|
||||
|
|
|
|||
|
|
@ -147,25 +147,26 @@ class SendGrid extends Base implements \Rhymix\Framework\Drivers\MailInterface
|
|||
$headers = array(
|
||||
'Authorization' => 'Bearer ' . $this->_config['api_token'],
|
||||
'Content-Type' => 'application/json',
|
||||
'User-Agent' => 'PHP',
|
||||
);
|
||||
$options = array(
|
||||
'timeout' => 8,
|
||||
'useragent' => 'PHP',
|
||||
);
|
||||
|
||||
// Send the API request.
|
||||
$request = \Requests::post(self::$_url, $headers, json_encode($data), $options);
|
||||
$response_code = intval($request->status_code);;
|
||||
$request = \Rhymix\Framework\HTTP::post(self::$_url, $data, $headers, [], $options);
|
||||
$status_code = $request->getStatusCode();
|
||||
$result = $request->getBody()->getContents();
|
||||
|
||||
// Parse the result.
|
||||
if (!$response_code)
|
||||
if (!$status_code)
|
||||
{
|
||||
$message->errors[] = 'SendGrid: Connection error: ' . $request->body;
|
||||
$message->errors[] = 'SendGrid: Connection error: ' . $result;
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ class Woorimail extends Base implements \Rhymix\Framework\Drivers\MailInterface
|
|||
* The API URL.
|
||||
*/
|
||||
protected static $_url = 'https://woorimail.com/index.php';
|
||||
protected static $_timeout = 5;
|
||||
|
||||
/**
|
||||
* Error codes and messages.
|
||||
|
|
@ -171,16 +172,12 @@ class Woorimail extends Base implements \Rhymix\Framework\Drivers\MailInterface
|
|||
$headers = array(
|
||||
'Accept' => 'application/json, text/javascript, */*; q=0.1',
|
||||
);
|
||||
$options = array(
|
||||
'timeout' => 5,
|
||||
'useragent' => 'PHP',
|
||||
);
|
||||
|
||||
// Send the API request.
|
||||
try
|
||||
{
|
||||
$request = \Requests::post(self::$_url, $headers, $data, $options);
|
||||
$result = @json_decode($request->body);
|
||||
$request = \Rhymix\Framework\HTTP::post(self::$_url, $data, $headers, [], ['timeout' => self::$_timeout]);
|
||||
$result = @json_decode($request->getBody()->getContents());
|
||||
}
|
||||
catch (\Requests_Exception $e)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ class iwinv extends Base implements \Rhymix\Framework\Drivers\SMSInterface
|
|||
* API endpoint URL (fallback if URL is not explicitly configured)
|
||||
*/
|
||||
const LEGACY_API_URL = 'https://sms.service.iwinv.kr/send/';
|
||||
const DEFAULT_TIMEOUT = 5;
|
||||
|
||||
/**
|
||||
* API specifications.
|
||||
|
|
@ -80,14 +81,13 @@ class iwinv extends Base implements \Rhymix\Framework\Drivers\SMSInterface
|
|||
public function send(array $messages, \Rhymix\Framework\SMS $original)
|
||||
{
|
||||
$status = true;
|
||||
$curl = null;
|
||||
|
||||
foreach ($messages as $i => $message)
|
||||
{
|
||||
// Authentication
|
||||
$headers = array(
|
||||
'Content-Type: multipart/form-data',
|
||||
'secret: ' . base64_encode($this->_config['api_key'] . '&' . $this->_config['api_secret']),
|
||||
'Content-Type' => 'multipart/form-data',
|
||||
'secret' => base64_encode($this->_config['api_key'] . '&' . $this->_config['api_secret']),
|
||||
);
|
||||
|
||||
// Sender and recipient
|
||||
|
|
@ -109,7 +109,7 @@ class iwinv extends Base implements \Rhymix\Framework\Drivers\SMSInterface
|
|||
$data['text'] = $message->content;
|
||||
|
||||
// Image attachment
|
||||
if ($message->image)
|
||||
if (!empty($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;
|
||||
}
|
||||
|
||||
// We need to use curl because Filehandler::getRemoteResource() doesn't work with this API for some reason.
|
||||
if (!$curl)
|
||||
{
|
||||
$curl = curl_init();
|
||||
}
|
||||
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);
|
||||
// Send the request.
|
||||
$request = \Rhymix\Framework\HTTP::post($api_url, $data, $headers, [], ['timeout' => self::DEFAULT_TIMEOUT]);
|
||||
$result = $request->getBody()->getContents();
|
||||
$status_code = $request->getStatusCode();
|
||||
|
||||
// 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;
|
||||
}
|
||||
elseif (trim($result) === '')
|
||||
|
|
@ -162,11 +153,6 @@ class iwinv extends Base implements \Rhymix\Framework\Drivers\SMSInterface
|
|||
}
|
||||
}
|
||||
|
||||
if ($curl)
|
||||
{
|
||||
@curl_close($curl);
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue