Skip to content

Instantly share code, notes, and snippets.

View mtdowling's full-sized avatar

Michael Dowling mtdowling

View GitHub Profile
@mtdowling
mtdowling / guzzle_async_curl_with_mock.php
Created January 15, 2012 16:49
Guzzle: async curl requests with mocked responses
<?php
include 'guzzle.phar';
use Guzzle\Http\Client;
use Guzzle\Http\Message\Response;
use Guzzle\Http\Plugin\BatchQueuePlugin;
use Guzzle\Http\Plugin\MockPlugin;
$client = new Client('http://www.test.com/');
@mtdowling
mtdowling / cron-mondays.php
Created January 16, 2012 18:34
get the next 5 mondays from now using cron expression
<?php
// See https://github.com/mtdowling/cron-expression
require 'cron.phar';
$totalWeeks = 5;
$fromDate = 'now';
$cron = Cron\CronExpression::factory('0 0 * * 1');
$dates = $cron->getMultipleRunDates($totalWeeks, $fromDate);
@mtdowling
mtdowling / gist:3877615
Created October 12, 2012 06:24
Iterative XML to array conversion
<?php
function iXml2array($xml)
{
$result = array();
$stack = array(array(&$result, &$xml));
do {
$frame = array_pop($stack);
@mtdowling
mtdowling / gist:3877616
Last active September 30, 2021 06:34
Recursive XML to array function
<?php
function xml2array($xml)
{
$arr = array();
foreach ($xml->getNamespaces() + array(null) as $prefix => $namespace) {
foreach ($xml->attributes($namespace) as $key => $value) {
// Add prefixes to prefixed attributes
if (is_string($prefix)) {
@mtdowling
mtdowling / gist:3877687
Created October 12, 2012 06:54
XML to array using json_encode
<?php
// Note that this will not maintain XML attributes
function jsonXml2Array(SimpleXMLElement $xml)
{
return json_decode(json_encode($xml), true);
}
function jsonXml2ArrayWithNamespacedAttributes($xml)
{
@mtdowling
mtdowling / gist:3877691
Created October 12, 2012 06:56
Eval XML to array
<?php
function evalXml2Array(SimpleXMLElement $xml)
{
eval('$x = ' . str_replace(array('SimpleXMLElement::__set_state(', '))'), array('', ')'), var_export($xml, true)) . ';');
return $x;
}
@mtdowling
mtdowling / gist:4516558
Last active March 7, 2018 00:56
Fibonacci in PHP using anonymous functions and memoization
<?php
$fibMemo = call_user_func(function () {
$memo = array(0 => 0, 1 => 1);
$fib = function ($n) use (&$memo, &$fib) {
if (!isset($memo[$n])) {
$memo[$n] = $fib($n - 1) + $fib($n - 2);
}
return $memo[$n];
};
@mtdowling
mtdowling / gist:4516616
Created January 12, 2013 07:42
Various methods of calculating Fibonacci numbers in PHP
<?php
/**
* Various methods of computing Fibonacci numbers in PHP
*/
class Fibonacci
{
/**
* @var array Memoization cache
* @see Fibonacci::memoized
@mtdowling
mtdowling / readfuction_perf.php
Created July 23, 2013 00:52
Simple script to reproduce CURLOPT_READFUNCTION performance issue
<?php
$m = curl_multi_init();
function sendMulti($ch)
{
global $m;
curl_multi_add_handle($m, $ch);
$active = false;
@mtdowling
mtdowling / simple.js
Created July 25, 2013 17:13
Simple node.js server
var http = require("http");
http.createServer(function(req, res) {
req.addListener("data", function(chunk) {
// Read data chunks
});
// Called when the request completes
req.addListener("end", function() {
res.writeHead(200, "OK", { "Content-Length": 10 });