1. Go to this page and download the library: Download ivan770/laravel-httpclient library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ivan770 / laravel-httpclient example snippets
// Obtaining instance via Facade alias
use HttpClient;
// You can use Facade class to access HttpClient
use Ivan770\HttpClient\Facades\HttpClient;
// Or, you can obtain HttpClient instance directly
use Ivan770\HttpClient\HttpClient;
public function method(HttpClient $client)
$response = $client->get("https://example.com");
$response = $client->get("https://example.com", ["query" => ["key" => "value"]]);
$response->getContent(); // Get response body, or collection, if response is JSON
$response->toCollection(); // Transform JSON response to collection
$response->getStatusCode(); // Get response status code
$response->getHeaders(); // Get response headers
// You can use HTTP request methods as client methods
$client->head("https://example.com");
$client->post("https://example.com", ["body" => ["key" => "value"]]);
$client->post("https://example.com", ["json" => ["key" => "value"]]);
$client->put("https://example.com");
$client->delete("https://example.com");
use Ivan770\HttpClient\HttpClient;
use Ivan770\HttpClient\Request\Request;
use Ivan770\HttpClient\Response\MockResponse;
class HttpBinGet extends Request
{
// Request URL
protected $resource = "https://httpbin.org/get";
// Request method
protected $method = "GET";
// This method is called on request init.
// Here, you are able to use builder to modify your request
protected function defaultAttach(HttpClient $client)
{
$client->authBearer("test");
}
protected function tests()
{
return [
"success" => MockResponse::make("Hello World!"),
];
}
}
// Execute request
app(HttpBinGet::class)->execute();
// Execute request and receive result
app(HttpBinGet::class)->get();
// Modify request using "attach" method.
app(HttpBinGet::class)->attach(function (HttpClient $client) {
$client->headers(["test" => true]);
})->execute();
// Request also forwards calls to HttpClient
app(HttpBinGet::class)->headers(["test" => true])->execute();
// Mock responses
$response = app(HttpBinGet::class)->mock("success");
$response->getContent(); // "Hello World!"
$response->getStatus(); // 200
$response->getHeaders(); // []
use Ivan770\HttpClient\Request\Request;
use Ivan770\HttpClient\Contracts\PassToBrowserKit;
// Just implement PassToBrowserKit contract to start using BrowserKit
class HttpBinGet extends Request implements PassToBrowserKit
{
// Request URL
protected $resource = "https://httpbin.org/get";
// Request method
protected $method = "GET";
}