📜 ⬆️ ⬇️

HttpRequest - a library for easy work with the HTTP protocol

https://github.com/Garik-/http-request

Very often in the code you can find the following lines:

$result = file_get_contents("http://geocode-maps.yandex.ru/1.x/?geocode=".urlencode("")); $handle = fopen("http://www.example.com/", "rb"); $result = fgets($handle); 


 try { $http = HttpRequest::get("http://geocode-maps.yandex.ru/1.x/?format=json",array("geocode"=>$city))->acceptJson(); $json = $http->ok() ? json_decode($http->body()) : null; } catch (HttpRequestException $e) { exit($e->getMessage()); } 

Now it really does not matter.
')
The Http Request library is based on the Kevin Sawicki library of the same name , which should be well known to Android developers, since GitHub itself uses it in its application.

Small examples

Submitting a form using the POST method with its own headers

 $http = HttpRequest::post("http://example.com/")->form( array( "param1" => "value", "param2" => "value", "file" => "@/home/vasya/attach.txt" )) ->header(HttpRequest::HEADER_USER_AGENT, "Opera/9.60 (J2ME/MIDP; Opera Mini/4.2.14912/812; U; ru)") ->header(HttpRequest::HEADER_REFERER,"http://google.com"); 

Sending a file using the PUT method

 $http = HttpRequest::put("http://example.com/")->upload("/home/vasja/attach.txt"); 

Display all server response headers

 print_r(HttpRequest::head("http://example.com")->headers()); 

File upload from server

 $image=fopen('image.jpg','wb'); $loaded=HttpRequest::get("http://example.com/file.jpg")->receive($image)->ok(); // boolean 

The library includes only 3 files, and can be a good alternative to monstrous frameworks for your own projects.

Source: https://habr.com/ru/post/187164/


All Articles