Http.php
2 weeks ago
HttpTransportCurl.php
2 weeks ago
HttpTransportFopen.php
2 weeks ago
HttpTransportInterface.php
2 weeks ago
HttpTransportWp.php
2 weeks ago
Http.php
55 lines
| 1 | <?php |
| 2 | |
| 3 | namespace LLAR\Core\Http; |
| 4 | |
| 5 | class Http { |
| 6 | |
| 7 | /** |
| 8 | * @var HttpTransportInterface |
| 9 | */ |
| 10 | private static $transport; |
| 11 | |
| 12 | /** |
| 13 | * @throws \Exception |
| 14 | */ |
| 15 | public static function init() { |
| 16 | |
| 17 | if( function_exists( 'fopen' ) && ini_get( 'allow_url_fopen' ) === '1' ) { |
| 18 | self::$transport = new HttpTransportFopen(); |
| 19 | } else if( function_exists( 'wp_remote_get' ) ) { |
| 20 | self::$transport = new HttpTransportWp(); |
| 21 | } else if( function_exists( 'curl_version' ) ) { |
| 22 | self::$transport = new HttpTransportCurl(); |
| 23 | } else { |
| 24 | throw new \Exception( 'Unable to determine HTTP transport.' ); |
| 25 | } |
| 26 | |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @param $url |
| 31 | * @param array $options |
| 32 | * |
| 33 | * @return mixed |
| 34 | */ |
| 35 | public static function get( $url, $options = array() ) { |
| 36 | |
| 37 | return self::$transport->get( $url, $options ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param $url |
| 42 | * @param array $options |
| 43 | * |
| 44 | * @return mixed |
| 45 | */ |
| 46 | public static function post( $url, $options = array() ) { |
| 47 | |
| 48 | $options['headers'] = array_merge( |
| 49 | array( 'Content-Type: application/json; charset=utf-8' ), |
| 50 | !empty( $options['headers'] ) ? $options['headers'] : array() |
| 51 | ); |
| 52 | |
| 53 | return self::$transport->post( $url, $options ); |
| 54 | } |
| 55 | } |