Skip to content

Instantly share code, notes, and snippets.

View christeredvartsen's full-sized avatar

Christer Edvartsen christeredvartsen

View GitHub Profile
@christeredvartsen
christeredvartsen / README.md
Created September 25, 2020 09:12
Simple retry strategy for Guzzle 7 when encountering "429 Too Many Requests" errors

Simple retry strategy for Guzzle 7

APIs might implement rate limiting, and if they do your clients might experience 429 Too Many Requests responses with a Retry-After header, informing your client how long it should wait before making the next request. And being good internet citizens we should all implement support for this in our API clients.

Guzzle includes a retry middleware class that can be used to handle this.

The implementation in this gist is a PoC, so feel free to build upon it, and comment if you think something should be added / removed.

@christeredvartsen
christeredvartsen / replace.php
Last active December 14, 2015 02:18
Replace _words_ in a string with <em>words</em> but not when they are inside an URL
<?php
// Lookbehinds require fixed length, but lookaheads does not, so just reverse the string
$string = 'some string with _foo_ and _bar_ and http://example.com/some_foo_bar';
$replaced = strrev(preg_replace('#_([a-zæøå -]+)_(?![^ ]+//:ptth)#i', '>me/<\1>me<', strrev($string)));
echo $replaced; // some string with <em>foo</em> and <em>bar</em> and http://example.com/some_foo_bar
@christeredvartsen
christeredvartsen / gist:800646
Created January 28, 2011 17:54
Traverse some directory
<?php
$path = '/some/dir';
$dir = new RecursiveDirectoryIterator($path);
$iterator = new RecursiveIteratorIterator($dir);
foreach ($iterator as $entry) {
print($entry->getRealPath() . PHP_EOL);
}