Skip to content

emka.web.id

Menu
  • Home
  • Indeks Artikel
  • Tutorial
  • Tentang Kami
Menu

Inilah Cara Mencatat Request dan Response Guzzle di Laravel

Posted on March 28, 2020 by Syauqi Wiryahasana
Guzzle seperti kita tahu adalah salah satu pilihan library PHP yang memberikan fungsionalitasi client http lebih mantap daripada client http bawaan PHP. Guzzle popular digunakan oleh banyak orang sebagai tool untuk melakukan scrapping data. Nah, dalam tutorial kali ini kita akan mencatat apa saja yang di request dan diperoleh Guzzle dari setiap aksinya. Fitur pencatatan atau logging ini sangat berguna untuk debuggin dikemudian hari. Untuk tutorial kali ini kita akan menggunakan Library dari Syed Sirajul Islam Anik (atau om Anik) di Github yaitu, Loguzz (log + guzzle). untuk instalasinya, tinggal pakai composer: [sourcecode]composer require anik/loguzz[/sourcecode] pada sourcecode untuk melakukan logging pada Guzzle, jangan lupa use Psr\Log\LoggerInterface dan juga use Loguzz\Middleware\LogMiddleware. kemudian tambahkan option-option seperti sourcecode berikut: [sourcecode] $options = [ // 'length' => 100, // 'log_request' => true, // 'log_request' => false, // 'log_response' => true, // 'log_response' => false, // 'success_only' => false, // 'exceptions_only' => false, // 'log_level' => 'notice', // 'request_formatter' => new RequestCurlFormatter, // 'response_formatter' => new ResponseJsonFormatter, ]; // for Laravel/Lumen, you can use $logger = app('logger'); $logger = new Logger('guzzle.to.curl'); $handlerStack = HandlerStack::create(); $handlerStack->push(new LogMiddleware($logger, $options), 'logger'); $client = new Client([ 'handler' => $handlerStack, // push the handler stack 'http_errors' => false ]); [/sourcecode] lalu silakan dicoba untuk melakukan request dengan Guzzle: [sourcecode] // then an HTTP request try { $response = $client->get('http://httpbin.org/get'); } catch(\Exception $e) {} [/sourcecode] selamat mencoba!
Seedbacklink

Recent Posts

TENTANG EMKA.WEB>ID

EMKA.WEB.ID adalah blog seputar teknologi informasi, edukasi dan ke-NU-an yang hadir sejak tahun 2011. Kontak: kontak@emka.web.id.

©2024 emka.web.id Proudly powered by wpStatically