Blog Spot!


PHP Curl functions

function makeGetRequest($url, $headers) {
    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1, // return the transfer as a string
        CURLOPT_FAILONERROR    => 0, // response code greater than 400 cause error
        CURLOPT_HEADER         => 1, // show curl response headers
        CURLOPT_HTTPHEADER     => $headers,
        CURLOPT_URL            => $url,
        CURLOPT_CONNECTTIMEOUT => 10, // should only spend 10 seconds attempting to connect
        CURLOPT_TIMEOUT        => 30, // hould only spend a maximum of 30 seconds executing the request
    ));

    $resp = curl_exec($curl);

    curl_close($curl);
    return $resp;
}

function makePostRequest($url, $headers, $body) {
    $curl = curl_init();

    curl_setopt_array( $curl, array(
        CURLOPT_RETURNTRANSFER => 1, // return the transfer as a string
        CURLOPT_FAILONERROR    => 0, // response code greater than 400 cause error
        CURLOPT_HEADER         => 1, // show curl response headers.
        CURLOPT_HTTPHEADER     => $headers,
        CURLOPT_URL            => $url,
        CURLOPT_POST           => 1,
        CURLOPT_POSTFIELDS     => $body,
        CURLOPT_CONNECTTIMEOUT => 10, // should only spend 10 seconds attempting to connect
        CURLOPT_TIMEOUT        => 30, // hould only spend a maximum of 30 seconds executing the request
    ));

    $resp = curl_exec($curl);

    curl_close($curl);
    return $resp;
}

Added on 01.Sep.2017
Tags: php function curl handler get post rest

600k concurrent websocket connections on AWS using Node.js

Added on 09.Aug.2017
Tags: nodejs settings cfg concurrent

Nonaggregated Column error, only in MySQL 5.7.5+

HOW TO

-- list all global variables
SHOW GLOBAL VARIABLES WHERE Variable_name = 'sql_mode'

..get sql_mode value - remove ONLY_FULL_GROUP_BY

-- save `sql_mode` new value
SET GLOBAL sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

must reconnect to work!

Added on 04.Aug.2017
Tags: sql mysql sql_mode

Extended implementation of async.waterfall

Extended implementation that allows functions to pass a varying number of arguments along.

function waterfall(tasks, cb) {
    cb = cb || function() {};

    // support for arbitrary # of arguments passed along by functions
    var data = Array.prototype.slice.call(arguments, 2);

    if (!Array.isArray(tasks)) {
        return cb('tasks must be an array');  
    }

    // check if there are functions left
    if (!tasks[0]) {

        // avoid race condition with 
        if (data.length > 1) {
            cb(null, data);
        }  else {
            cb(null, data[0]);
        }
        return;
    }

    // process the next function
    var next = tasks[0];
    data.push(function(err) {
        if (err) {
            return cb(err);
        }

        // build the args for waterfall...
        var args = [];
        // remaining functions    
        args.push(tasks.slice(1));
        // final waterfall callback
        args.push(cb);
        // arbitrary # of args for next function
        args.push.apply(args, Array.prototype.slice.call(arguments, 1)); 

        process.nextTick(function() {
            waterfall.apply(null, args);
        });
    });

    next.apply(null, data);
};
// test
waterfall([
    function(next) {
        console.log('1st');
        setTimeout(next, 1000);
    },

    function(next) {
        console.log('2nd');
        setTimeout(next, 1000);
    },

    function(next) {
        console.log('3rd');
        setTimeout(next, 1000);
    },

    function(next) {
        console.log('4th');
        next(null, 'Hello!');
    },
], function(err, res) {
    console.log(err, res);
});

jsfiddle

Added on 19.Jun.2017
Tags: js node async waterfall nodejs

Simple implementation of async.waterfall

function waterfall(arr, cb) {
    var fns = arr.slice(1);

    if (!arr[0]) {
        process.nextTick(function() {
            cb(null);
        });
        return;
    }

    var first = arr[0];

    first(function(err, data) {
         if(err) {
             return cb(err);
         }
         waterfall(fns, cb, data); 
    });
}

waterfall([
    function(next) {
        console.log('1st');
        setTimeout(next, 1000);
    },

    function(next) {
        console.log('2nd');
        setTimeout(next, 1000);
    },

    function(next) {
        console.log('3rd');
        setTimeout(next, 1000);
    },

    function(next) {
        console.log('4th');
        setTimeout(next, 1000);
    },
], function(err) {
    console.log(err);
});

no nodejs environment helper

var process = {nextTick:function(fn){ setTimeout(fn); }};

UPDATE: 2021-10-27

  • Enable value passing to next function here

stackoverflow

Added on 14.Jun.2017
Tags: js node async waterfall nodejs

Search


PHP Libraries


Carbon lib / docs
Idiorm lib / docs
Image Workshop lib / docs
lorenzos/Minixed lib / docs
Parsedown lib / docs
PHP Paginator lib / docs
PHP Redis lib / docs
QrCode lib / docs
Requests lib / docs
Slim lib / docs
Spyc lib / docs
TWIG lib / docs
Upload lib / docs
Validation lib / docs
Zebra Image lib / docs

JS Libraries


AJV lib / docs
BackboneJS lib / docs
Bootstrap Notify lib / docs
C3.js lib / docs
ChartJS lib / docs
FastMD5 lib / docs
HighlightJS lib / docs
jQuery-Storage lib / docs
JS-Cookie lib / docs
Leaflet JS lib / docs
LowDB lib / docs
Marked lib / docs
NeedlyJS lib / docs
ParcelJS lib / docs
RequireJS lib / docs
Swig lib / docs
Toastr lib / docs
Underscore lib / docs
ValidateJS lib / docs
top