francis94c /
ci-gmail
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | defined('BASEPATH') OR exit('No direct script access allowed');
|
||
| 4 | |||
| 5 | class GMailCURL {
|
||
| 6 | |||
| 7 | const GET = 'GET'; |
||
| 8 | const POST = 'POST'; |
||
| 9 | |||
| 10 | private $method; |
||
| 11 | private $userAgent; |
||
| 12 | |||
| 13 | function __construct(string $method, string $userAgent='CodeIgniter GMail API') {
|
||
| 14 | $this->method = $method; |
||
| 15 | $this->userAgent = $userAgent; |
||
| 16 | } |
||
| 17 | |||
| 18 | function __invoke(string $url, array $header=[], array $body=null):array {
|
||
|
0 ignored issues
–
show
|
|||
| 19 | if ($body != null) $body = json_encode($body); |
||
| 20 | |||
| 21 | $ch = curl_init($url); |
||
| 22 | |||
| 23 | // Defaults. |
||
| 24 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
| 25 | curl_setopt($ch, CURLINFO_HEADER_OUT, true); |
||
| 26 | if (ENVIRONMENT == 'development') {
|
||
| 27 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
||
| 28 | } |
||
| 29 | // Header. |
||
| 30 | $header[] = 'Content-Type: application/json'; |
||
| 31 | if ($body != null) {
|
||
| 32 | $header[] = 'Content-Length: '.strlen($body); |
||
| 33 | } else {
|
||
| 34 | $header[] = 'Content-Length: 0'; |
||
| 35 | } |
||
| 36 | curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
||
| 37 | curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); |
||
| 38 | // Request Method and Body. |
||
| 39 | if ($this->method == self::POST) {
|
||
| 40 | curl_setopt($ch, CURLOPT_POST, true); |
||
| 41 | if ($body != null) {
|
||
| 42 | curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | // Exec. |
||
| 46 | $response = curl_exec($ch); |
||
| 47 | $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
| 48 | curl_close($ch); |
||
| 49 | return [$code, $response]; |
||
| 50 | } |
||
| 51 | } |
||
| 52 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.