PHP code example of ksuzuki2016 / laravel-http-client

1. Go to this page and download the library: Download ksuzuki2016/laravel-http-client 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/ */

    

ksuzuki2016 / laravel-http-client example snippets

 shell script
php artisan dusk:chrome-driver 70
 php


return [

    'binPath' => env('HTTP_CLIENT_CHROME_PATH') ,

    /*
    |--------------------------------------------------------------------------
    | Http Request Driver
    |--------------------------------------------------------------------------
    | dusk is alias for dusk-chrome
    |
    | Drivers: "guzzle", "dusk" , "dusk-chrome"
    |
    */
    'default' => env('HTTP_CLIENT_DRIVER', 'guzzle') ,

    'http_facade_overwrite' => env('HTTP_FACADE_OVERWRITE', false ) ,

];
 php


use KSuzuki2016\HttpClient\DriverManager ;

$manager = app(KSuzuki2016\HttpClient\DriverManager::class) ;
$manager->driver('dusk')->get('URL') ;

// 又は

$manager = app('http-client') ;
$manager->get('URL') ;
 php

use KSuzuki2016\HttpClient\DriverManager ;

$manager = app(KSuzuki2016\HttpClient\DriverManager::class) ;
$manager->driver('dusk')->get('URL')->crawler() ;
$manager->driver('dusk')->get('URL')->stacks() ;
 php

use KSuzuki2016\HttpClient\DriverManager ;

$manager = app(KSuzuki2016\HttpClient\DriverManager::class) ;
$manager->get('URL')->schema() ;
 php

use KSuzuki2016\HttpClient\DriverManager ;

$manager = app(KSuzuki2016\HttpClient\DriverManager::class) ;
$manager->driver('dusk')->browserCallback( new BrowserMacro )->get('URL') ;
 php

namespace HttpClient\Macros;

use KSuzuki2016\HttpClient\Drivers\ChromeBrowser;

class TestMacro
{
    /**
     * @param ChromeBrowser $browser
     * @return void|string|null
     */
    public function __invoke(ChromeBrowser $browser)
    {
        // Browser Macro
    }
}
 php
$response->header('errors');
/*
unknown error: Runtime.evaluate threw exception: SyntaxError: Unexpected identifier
  (Session info: headless chrome=87.0.4280.66)
  (Driver info: chromedriver=70.0.3538.97 (d035916fe243477005bc95fe2a5778b8f20b6ae1),platform=Linux 4.15.0-96-generic x86_64) screenshot path to screen
*/
 php

use KSuzuki2016\HttpClient\DriverManager ;

$manager = app(KSuzuki2016\HttpClient\DriverManager::class) ;
$manager->driver('dusk')
        ->responseObserver( new ResponseLogObserver )
        ->responseObserver( new ResponseObserver )
        ->get('URL') ;
 php

namespace KSuzuki2016\HttpClient\Logging;

use KSuzuki2016\HttpClient\Contracts\ResponseObserver;
use KSuzuki2016\HttpClient\Http\Client\HttpClientResponse;

class ResponseLogObserver extends ResponseObserver
{
    public function successful(HttpClientResponse $response)
    {
        // 処理を書く
    }

    public function failed(HttpClientResponse $response)
    {
        // ResponseObserverを継承していれば
        // 以降の処理を止める場合にbreakObservationを呼び出して停止可能
        $this->breakObservation();
    }
}
 php
public function successful(HttpClientResponse $response)
{
    $stacks = array_merge( $response->stacks() , $response->json() ) ;
    $response->setStacks( $stacks ) ;
}
 php
public function successful(HttpClientResponse $response)
{
    return $response->setJson( 'key' , 'new value' ) ;
}
 shell script
$ php artisan make:http:macro CustomMacro
 shell script
$ php artisan make:http:Observer CustomObserver
 php

namespace App\HttpClients ;

use KSuzuki2016\HttpClient\Contracts\HttpClientRequest;

class MobileClient extends HttpClientRequest
{
    /**
     * observer brawserCallback の設定等を行う場合
     * プロパティでも設定可能
     * @property HttpClientFactory app
     * 
     * サービスプロバイダの bindings の様な設定も可能
     * @property array observers
     * @property array macros
    */

    protected $headers = [
        'user-agent' => 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.91 Mobile Safari/537.36'
    ] ;
}
 php
$client = new MobileClient ;
$client->get('http://...') ;

// ResponseObserver BrowserMacro の利用
$client->observe( ResponseObserver )->macro( BrowserMacro )->get('http://...') ;

// デバッグを行う場合
$client->debug()->get('http://...') ;