Skip to content

Instantly share code, notes, and snippets.

@mortenscheel
Created February 24, 2023 14:42
Show Gist options
  • Save mortenscheel/bdb010ba05921b51f6419959203defc8 to your computer and use it in GitHub Desktop.
Save mortenscheel/bdb010ba05921b51f6419959203defc8 to your computer and use it in GitHub Desktop.
Write raw request and response to file with Laravel's Http client
<?php
namespace App\Providers;
use GuzzleHttp\Middleware;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Http::macro('record', function(string $path, bool $append = true) {
return $this->withMiddleware(Middleware::tap(after: function($request, $options, $promise) use ($path, $append) {
$fh = fopen($path, $append ? 'ab+' : 'wb');
$record = fn(string $line, bool $newline = true) => fwrite($fh, $line . ($newline ? PHP_EOL : ''));
$record(sprintf('%s %s HTTP/%s', $request->getMethod(), $request->getRequestTarget(), $request->getProtocolVersion()));
foreach ($request->getHeaders() as $name => $values) {
$record(sprintf('%s: %s', $name, implode(', ', $values)));
}
$record('');
while ($request->getBody()->eof() !== true) {
$record($request->getBody()->read(1024), false);
}
$response = $promise->wait(true);
$record(sprintf('HTTP/%s %d %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase()));
foreach ($response->getHeaders() as $name => $values) {
$record(sprintf('%s: %s', $name, implode(', ', $values)));
}
$record('');
while ($response->getBody()->eof() !== true) {
$record($response->getBody()->read(1024), false);
}
fclose($fh);
}));
});
}
}
@mortenscheel
Copy link
Author

Example:

Http::record(storage_path('app/request.txt'))->get('https://www.google.com');
storage/app/request.txt
GET / HTTP/1.1
User-Agent: GuzzleHttp/7
Host: www.google.com

HTTP/1.1 200 OK
Date: Fri, 24 Feb 2023 14:45:53 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server: gws
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Set-Cookie: SOCS=CAAaBgiA_N-fBg; expires=Mon, 25-Mar-2024 14:45:53 GMT; path=/; domain=.google.com; Secure; SameSite=lax, AEC=ARSKqsKokdbGWJ4nGZXWexV9SVbEnCAz19ss-dlu8H6XHIyTArbEm4tYJQ; expires=Wed, 23-Aug-2023 14:45:53 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax, __Secure-ENID=10.SE=ShNkh2gm8LPKGXddJE1rskCuJMvuqoBIbfYrytUKW0BSYH5OQ-FdtP1iJOsoWHf6aV8N8v3th0szyLJwfErtJPXrTznXEOc6MzVyTyHRXLx2k-drF33M6JbnJpaAlgAJMwlcDCYerXT55USiGgU_n0m2uNwD7ISAq8XKXq8GXKc; expires=Tue, 26-Mar-2024 07:04:11 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax, CONSENT=PENDING+330; expires=Sun, 23-Feb-2025 14:45:53 GMT; path=/; domain=.google.com; Secure
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
Accept-Ranges: none
Vary: Accept-Encoding
Transfer-Encoding: chunked

<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="da"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title><script nonce="Tygfq1ABYeccQmW5kH4W3A">(function(){window.google={kEI:'oc34Y-6EEZS6xc8Pvd602AE',kEXPI:'0,1359409,6059,206,4804,2316,383,246,5,1129120,1197770,631,165,379925,16114,28684,22431,1361,12318,17581,4998,13228,3847,36218,3819,1279,2891,1103,11257,50059,7126,1102,18159,3,346,230,6459,149,13975,4,1528,2304,42125,11444,2216,4437,9358,7428,5827,2530,4094,7596,1,14261,27893,2,14022,25739,5679,1021,31122,4567,6256,23421,1252,5835,14967,4333,7484,25076,2006,8155,7381,14289,1681,873,6577,13056,9,1920,5784,3995,19130,2261,14763,2523,3782,2006,17769,424,5796,1,11826,2609,4641,15565,1622,1779,4976,4725,7740,2426,721,32,989,2355,3819,568,988,3030,427,5202,481,1411,20,3,867,136,24,78,7167,1804,567,6973,10,679,1150,1088,5,442,51,1155,109,68,613,446,450,403,1169,465,2530,2638,2273,1078,949,2,3,17,2,3,575,468,98,355,5674,212,2,375,537,2345,735,78,2085,669,1,354,773,928,1310,84,302,484,273,1296,34,1335,362,342,134,832,324,2481,34,1221,426,804,938,1990,15,2,10,3,1,26,219,1271,2,1924,310,150,177,414,304,94,51,487,141,130,92,146,199,546,381,1011,1232,232,18,1096,404,452,411,247,511,360,914,4,4,437,10,2305,98,202,1109,5233514,12,5914,103,43,167,40,5993769,2805074,3311,141,795,19735,1,303,44,4356,23946128,4042143,1964,16672,2894,512,5738,11103,2855,142,317',kBL:'OnVG'};google.sn='webhp';google.kHL='da';})();(function(){
var f=this||self;var h,k=[];function l(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));)a=a.parentNode;return b||h}function m(a){for(var b=null;a&&(!a.getAttribute||!(b=a.getAttribute("leid")));)a=a.parentNode;return b}
function n(a,b,c,d,g){var e="";c||-1!==b.search("&ei=")||(e="&ei="+l(d),-1===b.search("&lei=")&&(d=m(d))&&(e+="&lei="+d));d="";!c&&f._cshid&&-1===b.search("&cshid=")&&"slh"!==a&&(d="&cshid="+f._cshid);c=c||"/"+(g||"gen_204")+"?atyp=i&ct="+a+"&cad="+b+e+"&zx="+Date.now()+d;/^http:/i.test(c)&&"https:"===window.location.protocol&&(google.ml&&google.ml(Error("a"),!1,{src:c,glmm:1}),c="");return c};h=google.kEI;google.getEI=l;google.getLEI=m;google.ml=function(){return null};google.log=function(a,b,c,d,g){if(c=n(a,b,c,d,g)){a=new Image;var e=k.length;k[e]=a;a.onerror=a.onload=a.onabort=function(){delete k[e]};a.src=c}};google.logUrl=n;}).call(this);(function(){google.y={};google.sy=[];google.x=function(a,b){if(a)var c=a.id;else{do c=Math.random();while(google.y[c])}google.y[c]=[a,b];return!1};google.sx=function(a){google.sy.push(a)};google.lm=[];google.plm=function(a){google.lm.push.apply(google.lm,a)};google.lq=[];google.load=function(a,b,c){google.lq.push([[a],b,c])};google.loadAll=function(a,b){google.lq.push([a,b])};google.bx=!1;google.lx=function(){};}).call(this);google.f={};(function(){
document.documentElement.addEventListener("submit",function(b){var a;if(a=b.target){var c=a.getAttribute("data-submitfalse");a="1"===c||"q"===c&&!a.elements.q.value?!0:!1}else a=!1;a&&(b.preventDefault(),b.stopPropagation())},!0);document.documentElement.addEventListener("click",function(b){var a;a:{for(a=b.target;a&&a!==document.documentElement;a=a.parentElement)if("A"===a.tagName){a="1"===a.getAttribute("data-nohref");break a}a=!1}a&&b.preventDefault()},!0);}).call(this);</script><style>#gbar,#guser{font-size:13px;padding-top:1px !important;}#gbar{height:22px}#guser{padding-bottom:7px !important;text-align:right}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}@media all{.gb1{height:22px;margin-right:.5em;vertical-align:top}#gbar{float:left}}a.gb1,a.gb4{text-decoration:underline !important}a.gb1,a.gb4{color:#00c !important}.gbi .gb4{color:#dd8e27 !important}.gbf .gb4{color:#900 !important}
</style><style>body,td,a,p,.h{font-family:arial,sans-serif}body{margin:0;overflow-y:scroll}#gog{padding:3px 8px 0}td{line-height:.8em}.gac_m td{line-height:17px}form{margin-bottom:20px}.h{color:#1558d6}em{font-weight:bold;font-style:normal}.lst{height:25px;width:496px}.gsfi,.lst{font:18px arial,sans-serif}.gsfs{font:17px arial,sans-serif}.ds{display:inline-box;display:inline-block;margin:3px 0 4px;margin-left:4px}input{font-family:inherit}body{background:#fff;color:#000}a{color:#4b11a8;text-decoration:none}a:hover,a:active{text-decoration:underline}.fl a{color:#1558d6}a:visited{color:#4b11a8}.sblc{padding-top:5px}.sblc a{display:block;margin:2px 0;margin-left:13px;font-size:11px}.lsbb{background:#f8f9fa;border:solid 1px;border-color:#dadce0 #70757a #70757a #dadce0;height:30px}.lsbb{display:block}#WqQANb a{display:inline-block;margin:0 12px}.lsb{background:url(/images/nav_logo229.png) 0 -261px repeat-x;border:none;color:#000;cursor:pointer;height:30px;margin:0;outline:0;font:15px arial,sans-serif;vertical-align:top}.lsb:active{background:#dadce0}.lst:focus{outline:none}.Ucigb{width:458px}</style><script nonce="Tygfq1ABYeccQmW5kH4W3A">(function(){window.google.erd={jsr:1,bv:1749,de:true};
var h=this||self;var k,l=null!=(k=h.mei)?k:1,n,p=null!=(n=h.sdo)?n:!0,q=0,r,t=google.erd,v=t.jsr;google.ml=function(a,b,d,m,e){e=void 0===e?2:e;b&&(r=a&&a.message);if(google.dl)return google.dl(a,e,d),null;if(0>v){window.console&&console.error(a,d);if(-2===v)throw a;b=!1}else b=!a||!a.message||"Error loading script"===a.message||q>=l&&!m?!1:!0;if(!b)return null;q++;d=d||{};b=encodeURIComponent;var c="/gen_204?atyp=i&ei="+b(google.kEI);google.kEXPI&&(c+="&jexpid="+b(google.kEXPI));c+="&srcpg="+b(google.sn)+"&jsr="+b(t.jsr)+"&bver="+b(t.bv);var f=a.lineNumber;void 0!==f&&(c+="&line="+f);var g=
a.fileName;g&&(0<g.indexOf("-extension:/")&&(e=3),c+="&script="+b(g),f&&g===window.location.href&&(f=document.documentElement.outerHTML.split("\n")[f],c+="&cad="+b(f?f.substring(0,300):"No script found.")));c+="&jsel="+e;for(var u in d)c+="&",c+=b(u),c+="=",c+=b(d[u]);c=c+"&emsg="+b(a.name+": "+a.message);c=c+"&jsst="+b(a.stack||"N/A");12288<=c.length&&(c=c.substr(0,12288));a=c;m||google.log(0,"",a);return a};window.onerror=function(a,b,d,m,e){r!==a&&(a=e instanceof Error?e:Error(a),void 0===d||"lineNumber"in a||(a.lineNumber=d),void 0===b||"fileName"in a||(a.fileName=b),google.ml(a,!1,void 0,!1,"SyntaxError"===a.name||"SyntaxError"===a.message.substring(0,11)||-1!==a.message.indexOf("Script error")?3:0));r=null;p&&q>=l&&(window.onerror=null)};})();</script></head><body bgcolor="#fff"><script nonce="Tygfq1ABYeccQmW5kH4W3A">(function(){var src='/images/nav_logo229.png';var iesg=false;document.body.onload = function(){window.n && window.n();if (document.images){new Image().src=src;}
if (!iesg){document.f&&document.f.q.focus();document.gbqf&&document.gbqf.q.focus();}
}
})();</script><div id="mngb"><div id=gbar><nobr><b class=gb1>Søgning</b> <a class=gb1 href="https://www.google.dk/imghp?hl=da&tab=wi">Billeder</a> <a class=gb1 href="https://maps.google.dk/maps?hl=da&tab=wl">Maps</a> <a class=gb1 href="https://play.google.com/?hl=da&tab=w8">Play</a> <a class=gb1 href="https://www.youtube.com/?tab=w1">YouTube</a> <a class=gb1 href="https://news.google.com/?tab=wn">Nyheder</a> <a class=gb1 href="https://mail.google.com/mail/?tab=wm">Gmail</a> <a class=gb1 href="https://drive.google.com/?tab=wo">Drev</a> <a class=gb1 style="text-decoration:none" href="https://www.google.dk/intl/da/about/products?tab=wh"><u>Mere</u> &raquo;</a></nobr></div><div id=guser width=100%><nobr><span id=gbn class=gbi></span><span id=gbf class=gbf></span><span id=gbe></span><a href="http://www.google.dk/history/optout?hl=da" class=gb4>Webhistorik</a> | <a  href="/preferences?hl=da" class=gb4>Indstillinger</a> | <a target=_top id=gb_70 href="https://accounts.google.com/ServiceLogin?hl=da&passive=true&continue=https://www.google.com/&ec=GAZAAQ" class=gb4>Log ind</a></nobr></div><div class=gbh style=left:0></div><div class=gbh style=right:0></div></div><center><br clear="all" id="lgpd"><div id="lga"><img alt="Google" height="92" src="/images/branding/googlelogo/1x/googlelogo_white_background_color_272x92dp.png" style="padding:28px 0 14px" width="272" id="hplogo"><br><br></div><form action="/search" name="f"><table cellpadding="0" cellspacing="0"><tr valign="top"><td width="25%">&nbsp;</td><td align="center" nowrap=""><input name="ie" value="ISO-8859-1" type="hidden"><input value="da" name="hl" type="hidden"><input name="source" type="hidden" value="hp"><input name="biw" type="hidden"><input name="bih" type="hidden"><div class="ds" style="height:32px;margin:4px 0"><div style="position:relative;zoom:1"><input class="lst Ucigb" style="margin:0;padding:5px 8px 0 6px;vertical-align:top;color:#000;padding-right:38px" autocomplete="off" value="" title="Google-søgning" maxlength="2048" name="q" size="57"><img src="/textinputassistant/tia.png" style="position:absolute;cursor:pointer;right:5px;top:4px;z-index:300" data-script-url="/textinputassistant/11/da_tia.js" id="tsuid_1" alt="" height="23" width="27"><script nonce="Tygfq1ABYeccQmW5kH4W3A">(function(){var id='tsuid_1';document.getElementById(id).onclick = function(){var s = document.createElement('script');s.src = this.getAttribute('data-script-url');(document.getElementById('xjsc')||document.body).appendChild(s);};})();</script></div></div><br style="line-height:0"><span class="ds"><span class="lsbb"><input class="lsb" value="Google-søgning" name="btnG" type="submit"></span></span><span class="ds"><span class="lsbb"><input class="lsb" id="tsuid_2" value="Jeg prøver lykken" name="btnI" type="submit"><script nonce="Tygfq1ABYeccQmW5kH4W3A">(function(){var id='tsuid_2';document.getElementById(id).onclick = function(){if (this.form.q.value){this.checked = 1;if (this.form.iflsig)this.form.iflsig.disabled = false;}
else top.location='/doodles/';};})();</script><input value="AK50M_UAAAAAY_jbsUX4tEyrZfQSeL3BTmim88zrppRO" name="iflsig" type="hidden"></span></span></td><td class="fl sblc" align="left" nowrap="" width="25%"><a href="/advanced_search?hl=da&amp;authuser=0">Avanceret søgning</a></td></tr></table><input id="gbv" name="gbv" type="hidden" value="1"><script nonce="Tygfq1ABYeccQmW5kH4W3A">(function(){var a,b="1";if(document&&document.getElementById)if("undefined"!=typeof XMLHttpRequest)b="2";else if("undefined"!=typeof ActiveXObject){var c,d,e=["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"];for(c=0;d=e[c++];)try{new ActiveXObject(d),b="2"}catch(h){}}a=b;if("2"==a&&-1==location.search.indexOf("&gbv=2")){var f=google.gbvu,g=document.getElementById("gbv");g&&(g.value=a);f&&window.setTimeout(function(){location.href=f},0)};}).call(this);</script></form><div id="gac_scont"></div><div style="font-size:83%;min-height:3.5em"><br><div id="gws-output-pages-elements-homepage_additional_languages__als"><style>#gws-output-pages-elements-homepage_additional_languages__als{font-size:small;margin-bottom:24px}#SIvCob{color:#3c4043;display:inline-block;line-height:28px;}#SIvCob a{padding:0 3px;}.H6sW5{display:inline-block;margin:0 2px;white-space:nowrap}.z4hgWe{display:inline-block;margin:0 2px}</style><div id="SIvCob">Google er tilgængelig på:  <a href="https://www.google.com/setprefs?sig=0_kW_L4z1euCFy8kdZWATcOsH_9uw%3D&amp;hl=fo&amp;source=homepage&amp;sa=X&amp;ved=0ahUKEwiu2aj9sq79AhUUXfEDHT0vDRsQ2ZgBCAU">Føroyskt</a>  </div></div></div><span id="footer"><div style="font-size:10pt"><div style="margin:19px auto;text-align:center" id="WqQANb"><a href="/intl/da/ads/">Annoncering</a><a href="http://www.google.dk/intl/da/services/">Virksomhedsløsninger</a><a href="/intl/da/about.html">Alt om Google</a><a href="https://www.google.com/setprefdomain?prefdom=DK&amp;prev=https://www.google.dk/&amp;sig=K_OSqOrvQLDLTVQa7nJurWiBEQpUg%3D">Google.dk</a></div></div><p style="font-size:8pt;color:#70757a">&copy; 2023 - <a href="/intl/da/policies/privacy/">Privatliv</a> - <a href="/intl/da/policies/terms/">Vilkår</a></p></span></center><script nonce="Tygfq1ABYeccQmW5kH4W3A">(function(){window.google.cdo={height:757,width:1440};(function(){var a=window.innerWidth,b=window.innerHeight;if(!a||!b){var c=window.document,d="CSS1Compat"==c.compatMode?c.documentElement:c.body;a=d.clientWidth;b=d.clientHeight}a&&b&&(a!=google.cdo.width||b!=google.cdo.height)&&google.log("","","/client_204?&atyp=i&biw="+a+"&bih="+b+"&ei="+google.kEI);}).call(this);})();</script> <script nonce="Tygfq1ABYeccQmW5kH4W3A">(function(){google.xjs={ck:'xjs.hp.FiypOaOGiwM.L.X.O',cs:'ACT90oElnxXPPx9BC3uRgISk6dA8tWPG3w',excm:[]};})();</script>  <script nonce="Tygfq1ABYeccQmW5kH4W3A">(function(){var u='/xjs/_/js/k\x3dxjs.hp.en.2WQUkfEOhJ0.O/am\x3dAACgEwBAAYAF/d\x3d1/ed\x3d1/rs\x3dACT90oGivWdokVbbNB0ZVZdxAEeP4yMcVA/m\x3dsb_he,d';var amd=0;
var d=this||self,e=function(a){return a};var g;var l=function(a,b){this.g=b===h?a:""};l.prototype.toString=function(){return this.g+""};var h={};
function m(){var a=u;google.lx=function(){p(a);google.lx=function(){}};google.bx||google.lx()}
function p(a){google.timers&&google.timers.load&&google.tick&&google.tick("load","xjsls");var b=document;var c="SCRIPT";"application/xhtml+xml"===b.contentType&&(c=c.toLowerCase());c=b.createElement(c);a=null===a?"null":void 0===a?"undefined":a;if(void 0===g){b=null;var k=d.trustedTypes;if(k&&k.createPolicy){try{b=k.createPolicy("goog#html",{createHTML:e,createScript:e,createScriptURL:e})}catch(q){d.console&&d.console.error(q.message)}g=b}else g=b}a=(b=g)?b.createScriptURL(a):a;a=new l(a,h);c.src=
a instanceof l&&a.constructor===l?a.g:"type_error:TrustedResourceUrl";var f,n;(f=(a=null==(n=(f=(c.ownerDocument&&c.ownerDocument.defaultView||window).document).querySelector)?void 0:n.call(f,"script[nonce]"))?a.nonce||a.getAttribute("nonce")||"":"")&&c.setAttribute("nonce",f);document.body.appendChild(c);google.psa=!0};google.xjsu=u;setTimeout(function(){0<amd?google.caft(function(){return m()},amd):m()},0);})();function _DumpException(e){throw e;}
function _F_installCss(c){}
(function(){google.jl={blt:'none',chnk:0,dw:false,dwu:true,emtn:0,end:0,ico:false,ikb:0,ine:false,injs:'none',injt:0,injth:0,injv2:false,lls:'default',pdt:0,rep:0,snet:true,strt:0,ubm:false,uwp:true};})();(function(){var pmc='{\x22d\x22:{},\x22sb_he\x22:{\x22agen\x22:true,\x22cgen\x22:true,\x22client\x22:\x22heirloom-hp\x22,\x22dh\x22:true,\x22ds\x22:\x22\x22,\x22fl\x22:true,\x22host\x22:\x22google.com\x22,\x22jsonp\x22:true,\x22msgs\x22:{\x22cibl\x22:\x22Ryd søgning\x22,\x22dym\x22:\x22Mente du:\x22,\x22lcky\x22:\x22Jeg prøver lykken\x22,\x22lml\x22:\x22Få flere oplysninger\x22,\x22psrc\x22:\x22Denne søgning blev fjernet fra din \\u003Ca href\x3d\\\x22/history\\\x22\\u003EWebhistorik\\u003C/a\\u003E\x22,\x22psrl\x22:\x22Fjern\x22,\x22sbit\x22:\x22Søg efter billede\x22,\x22srch\x22:\x22Google-søgning\x22},\x22ovr\x22:{},\x22pq\x22:\x22\x22,\x22rfs\x22:[],\x22sbas\x22:\x220 3px 8px 0 rgba(0,0,0,0.2),0 0 0 1px rgba(0,0,0,0.08)\x22,\x22stok\x22:\x22j_EK6XbXn1mmR5naLA2sLtR0Oa4\x22}}';google.pmc=JSON.parse(pmc);})();</script>        </body></html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment