Skip to content

Instantly share code, notes, and snippets.

@juliandavidmr
Created March 6, 2018 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliandavidmr/3aea6972e98db655a13f4e616e6dcebe to your computer and use it in GitHub Desktop.
Save juliandavidmr/3aea6972e98db655a13f4e616e6dcebe to your computer and use it in GitHub Desktop.
(PHP) Request Http without CURL
<?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