Skip to content

Commit

Permalink
Merge pull request #12 from utopia-php/fix-xxx-form-urlencoded
Browse files Browse the repository at this point in the history
Fix adapters using application/x-www-form-urlencoded content type
  • Loading branch information
abnegate authored Feb 7, 2023
2 parents 3a0d6b9 + 0e5e9ba commit a75d66d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
15 changes: 7 additions & 8 deletions src/Utopia/Messaging/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,25 @@ protected function request(
string $method,
string $url,
array $headers = [],
mixed $body = null,
?string $body = null,
): string {
$headers[] = 'Content-length: '.\strlen($body);

$ch = \curl_init();

if (! \is_null($body)) {
$headers[] = 'Content-Length: '.\strlen($body);
\curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}

\curl_setopt($ch, CURLOPT_URL, $url);
\curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
\curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
\curl_setopt($ch, CURLOPT_USERAGENT, "Appwrite {$this->getName()} Message Sender");

if (! is_null($body)) {
\curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}

$response = \curl_exec($ch);

if (\curl_errno($ch)) {
throw new \Exception('Error:'.\curl_error($ch));
throw new \Exception('Error: '.\curl_error($ch));
}
if (\curl_getinfo($ch, CURLINFO_HTTP_CODE) >= 400) {
throw new \Exception($response);
Expand Down
4 changes: 2 additions & 2 deletions src/Utopia/Messaging/Adapters/Email/Mailgun.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ protected function process(Email $message): string
headers: [
'Authorization: Basic '.base64_encode('api:'.$this->apiKey),
],
body: [
body: \http_build_query([
'from' => $message->getFrom(),
'to' => \implode(',', $message->getTo()),
'subject' => $message->getSubject(),
'text' => $message->isHtml() ? null : $message->getContent(),
'html' => $message->isHtml() ? $message->getContent() : null,
],
]),
);
}
}
4 changes: 2 additions & 2 deletions src/Utopia/Messaging/Adapters/SMS/Telesign.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ protected function process(SMS $message): string
headers: [
'Authorization: Basic '.base64_encode("{$this->username}:{$this->password}"),
],
body: [
body: \http_build_query([
'template' => $message->getContent(),
'recipients' => \implode(',', $to),
],
]),
);
}
}
4 changes: 2 additions & 2 deletions src/Utopia/Messaging/Adapters/SMS/TextMagic.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ protected function process(SMS $message): string
"X-TM-Username: {$this->username}",
"X-TM-Key: {$this->apiKey}",
],
body: [
body: \http_build_query([
'text' => $message->getContent(),
'from' => \ltrim($message->getFrom(), '+'),
'phones' => \implode(',', $to),
],
]),
);
}
}
4 changes: 2 additions & 2 deletions src/Utopia/Messaging/Adapters/SMS/Vonage.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ protected function process(SMS $message): string
return $this->request(
method: 'POST',
url: 'https://rest.nexmo.com/sms/json',
body: [
body: \http_build_query([
'text' => $message->getContent(),
'from' => $message->getFrom(),
'to' => \implode(',', $to),
'api_key' => $this->apiKey,
'api_secret' => $this->apiSecret,
]
]),
);
}
}

0 comments on commit a75d66d

Please sign in to comment.