Created
March 6, 2018 19:38
-
-
Save juliandavidmr/3aea6972e98db655a13f4e616e6dcebe to your computer and use it in GitHub Desktop.
(PHP) Request Http without CURL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace common\components; | |
class Request { | |
public static function get($url, $data = null, $header = null) { | |
return self::request("GET", $url, $data, $header); | |
} | |
public static function post($url, $data = null, $header = null) { | |
return self::request("POST", $url, $data, $header); | |
} | |
/** | |
* | |
* @param string $method | |
* Metodo de petición, ie: POST, GET, PUT | |
* @param string $url | |
* Direccion del recurso a solicitar | |
* @param array $data | |
* Datos adjuntos al paquete, podria ser un array para FormData | |
* @param string $header | |
* Cabecera de la petición, ie: Content-type: application/Json - Por defecto: Content-type: application/x-www-form-urlencoded | |
*/ | |
private static function request($method, $url, $data, $header) { | |
$method = empty($method) ? "GET" : $method; | |
$header = empty($header) ? "Content-type: application/x-www-form-urlencoded" : $header; | |
$options = array( | |
'https' => array( | |
'method' => $method, | |
'header' => $header, | |
'content' => http_build_query($data) | |
) | |
); | |
$context = stream_context_create($options); | |
return file_get_contents($url, false, $context); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment