PHP code example of ivan770 / laravel-httpclient

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";
}

// Add data to request
$client->query(["key" => "value"])->get("https://example.com")
$client->body(["key" => "value"])->post("https://example.com")
$client->json(["key" => "value"])->post("https://example.com")

// Add custom headers to request
$client->headers(["key" => "value"])->get("https://example.com");

// Ignore redirects
$client->withoutRedirects()->get("https://example.com");

// Proxy
$client->proxy("https://hostname:8080")->get("https://example.com");

// Authentication
$client->auth("auth_basic", ["username", "password"])->get("https://example.com");
$client->authBasic(["username", "password"])->get("https://example.com");
$client->authBearer("tokenhere")->get("https://example.com");

$model = User::find(1);
$client->parse($model)->post("https://example.com");

$response = $client->get("https://example.com");

// Pass data to Closure
$response->then(function ($data) {
    return $data;
});

// Use Laravel pipelines
$pipes = [
    ExamplePipe::class
];
$response->pipeline()->through($pipes)->then(function ($data) {
    return $data;
});