PHP code example of ehsandevs / service-proxy

1. Go to this page and download the library: Download ehsandevs/service-proxy 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/ */

    

ehsandevs / service-proxy example snippets


return [
    /**
     * Headers added to every request
     */
    'global_headers' => [
        'Accept' => 'application/json',
        ...
    ],

    'base_url' => env('PROXY_BASE_URL', env('APP_URL')),
];

use Ehsandevs\ServiceProxy\Proxy;

// Http Get
Proxy::withToken('Your bearer token')
    ->acceptJson()
    ->retry(3)
    ->withHeaders([
        "Content-Type" => "application\json"
    ])->get('api/articles');
    
Proxy::post('api/articles', [
    "title" => "Test title",
    "body" => "Test body"
]);

Proxy::patch('api/articles/1', [
    "title" => "Test title",
    "body" => "Test body"
]);

Proxy::put('api/articles', [
    "title" => "Test title",
    "body" => "Test body"
]);

Proxy::delete('api/articles/1');


use Ehsandevs\ServiceProxy\Proxy;
use Illuminate\Http\Request;

public function index(Request $request) {
    $serviceName = 'test-service';
    Proxy::request($request, $serviceName);
}


use Ehsandevs\ServiceProxy\Proxy;
use Ehsandevs\ServiceProxy\Responses\ProxyResponse;
 
Proxy::get('api/articles/1')->onSuccess(function (ProxyResponse $proxyResponse) {
        $data = $proxyResponse->data();
        $message = $proxyResponse->message();
        $response = $proxyResponse->response();
        $items = $proxyResponse->items();
        $count = $proxyResponse->count();
        ...
    });

use Ehsandevs\ServiceProxy\Proxy;
use Ehsandevs\ServiceProxy\Exceptions\ProxyException;
 
Proxy::get('api/articles/1')->onSuccess(function (ProxyException $proxyException) {
        $proxyResponse = $proxyException->proxyResponse;
        $trace = $proxyException->getTraceAsString();
        ...
    });

use Ehsandevs\ServiceProxy\Proxy;
 
Proxy::get('api/articles/1')->onDataSuccess(function (array $data) {
        $id = $data['id'];
    });

use Ehsandevs\ServiceProxy\Proxy;
 
Proxy::get('api/articles/1')->onCollectionSuccess(function (array $items, int $count) {
        ...
    });

use Ehsandevs\ServiceProxy\Proxy;

$proxyResponse = Proxy::get('api/articles/1');

use Ehsandevs\ServiceProxy\Proxy;
Proxy::mock('response.json')->get('address');
bash
php artisan vendor:publish --provider="Ehsandevs\ServiceProxy\Providers\ProxyServiceProvider" --tag config