HttpRequest-HTTPプロトコルを使用した簡単な作業のためのライブラリ

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



非常に頻繁にコードでこのような行を見つけることができます:



$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()); }
      
      





今では本当に問題ではありません。



Http Requestライブラリは、同じ名前のKevin Sawickiライブラリに基づいてます。これは、アプリケーションでGitHubによって使用されるため、Android開発者によく知られているはずです。



小さな例


独自のヘッダーを使用したPOSTフォーム送信


 $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");
      
      





PUTメソッドを使用してファイルを送信する


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





すべてのサーバー応答ヘッダーを印刷します


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





サーバーからファイルをダウンロードする


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





ライブラリには3つのファイルしか含まれていないため、独自のプロジェクトの巨大なフレームワークに代わる優れたファイルになる可能性があります。



All Articles