Erik's blog

Code, notes, recipes, general musings

Archive for December 2008

php multi-curl util

leave a comment »

motivation:
– simplify code by using a common method for making single- or multi-curl requests

usage:
– create a file, e.g. “curl.inc”, on your server and copy the code below w/ comment “multi curl util” into it
– create another file, e.g. “curl_test.php”, on your server and copy the code below w/ comment “test” into it
– browse to yourdomain.com/yourpath/curl_test.php
– comment-out or -in the various examples in the test code to see some common use cases

notes:
– only get and post are supported
– if you see a way to improve the code, please leave a comment.

<?php
//multi curl util
function create_get_handle($url, $params){
    $ch = curl_init();
    //format url
    foreach($params as $k => $v){
        $pairs[] = "$k=$v";
    }
    $param_str = implode('&', $pairs);
    $url .= '?'.$param_str;
    //set options
	$options = array(
		CURLOPT_URL => $url,
	    CURLOPT_HEADER => false,
		CURLOPT_RETURNTRANSFER => true
	);
	curl_setopt_array($ch, $options);
	return $ch;
}

function create_post_handle($url, $params){
    $ch = curl_init();
    //format params
    foreach($params as $k => $v){
        $pairs[] = "$k=$v";
    }
    $params = implode('&', $pairs);
    //set options	
	$options = array(
		CURLOPT_URL => $url,
	    CURLOPT_POST=> true,
		CURLOPT_POSTFIELDS => $params,
		CURLOPT_RETURNTRANSFER => true,
	);
	curl_setopt_array($ch, $options);
	return $ch;
}

function curl($requests){
    $mh = curl_multi_init();
    //prep each request
    foreach($requests as $index => $request){
        switch($request['method']){
            case 'post':
                $chs[$index] = create_post_handle($request['url'], $request['params']);
                break;
            case 'get':
                $chs[$index] = create_get_handle($request['url'], $request['params']);
                break;
        }
        curl_multi_add_handle($mh,$chs[$index]);
    }
    //credit: http://www.phpied.com/simultaneuos-http-requests-in-php-with-curl/
    // execute the handles
    $running = null;
    do {
        curl_multi_exec($mh, $running);
    } while($running > 0);
    // get content and remove handles
    foreach($chs as $index => $ch) {
        $results[$index] = curl_multi_getcontent($ch);
        curl_multi_remove_handle($mh, $ch);
    }
    // all done
    curl_multi_close($mh);
    return $results;
}
<?php
//test
if($_REQUEST){//if a request has been made, return request details
    echo '<p>request method: '.$_SERVER['REQUEST_METHOD'].'<br/>';
    echo 'request uri: '.$_SERVER['REQUEST_URI'].'<br/>';
    echo 'request vars: '.print_r($_REQUEST, true).'</p>';
    exit();
}

require('curl.inc');
$url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'];//have script call itself

/*
//single get
$requests[] = array(
    'method' => 'get',
    'url' => $url,
    'params' => array('x' => 'y')
);
/**/

/*
//single post
$requests[] = array(
    'method' => 'post',
    'url' => $url,
    'params' => array(
        'x' => 'y',
        'a' => 'b',
    )
);
/**/

/*
//multi get
$requests[] = array(
    'method' => 'get',
    'url' => $url,
    'params' => array('x' => 'y')
);
$requests[] = array(
    'method' => 'get',
    'url' => $url,
    'params' => array('x' => 'y')
);
/**/

/*
//multi post
$requests[] = array(
    'method' => 'post',
    'url' => $url,
    'params' => array(
        'x' => 'y',
        'a' => 'b',
    )
);
$requests[] = array(
    'method' => 'post',
    'url' => $url,
    'params' => array(
        'x' => 'y',
        'a' => 'b',
    )
);
/**/


//multi w/ get & post
$requests[] = array(
    'method' => 'get',
    'url' => $url,
    'params' => array('x' => 'y')
);
$requests[] = array(
    'method' => 'post',
    'url' => $url,
    'params' => array(
        'x' => 'y',
        'a' => 'b',
    )
);
/**/

$results = curl($requests);
var_dump($results);
Advertisements

Written by Erik

December 30, 2008 at 10:54 pm

Posted in code

Tagged with , ,

mini RESTful interface framework

leave a comment »

usage:

  • build out function stubs
  • put up on server

Written by Erik

December 20, 2008 at 4:09 am

Posted in code

Tagged with , ,

App Engine Y!AP app that pushes updates via OpenSocial JS API

leave a comment »

Usage:

  1. create App Engine app
  2. edit main.py to look like the code below and deploy
  3. create YAP app
  4. set app base url to yourappname.appspot.com/example
  5. preview your app
import wsgiref.handlers
from google.appengine.ext import webapp

class ExampleHandler(webapp.RequestHandler):
	def post(self):
		html = """
		<script>
		//ref: http://developer.yahoo.com/yap/guide/opensocial-examples.html
		var postActivity = function(title, body) {
				var params = {};
				params[opensocial.Activity.Field.TITLE] = title;
				params[opensocial.Activity.Field.BODY] = body;
				var activity = opensocial.newActivity(params);
				opensocial.requestCreateActivity(
					activity,
					opensocial.CreateActivityPriority.LOW,
					function(){});
			},
			handleResponse = function(response){

				var viewer = response.get('viewer').getData(),
					name = viewer.getDisplayName();

				postActivity(
					name + ' posted an update ...',
					'... using OpenSocial!'
				);
			},
			getViewerData = function() {
				var req = opensocial.newDataRequest();
			  	req.add(req.newFetchPersonRequest("VIEWER"), "viewer");
				req.send(handleResponse);
			};

		//this is the bare minimum code to push updates
		var params = {};
		params[opensocial.Activity.Field.TITLE] = 'title';
		params[opensocial.Activity.Field.BODY] = 'body';
		var activity = opensocial.newActivity(params);
		opensocial.requestCreateActivity(
			activity,
			opensocial.CreateActivityPriority.LOW,
			function(){});

		//this is a slightly enhanced update flow
		getViewerData();

		</script>
		"""
		self.response.headers['Content-Type'] = 'text/html'
		self.response.out.write(html)

application = webapp.WSGIApplication(
	[('/example', ExampleHandler)],
	debug=True)

def main():
	wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
 main()

Written by Erik

December 19, 2008 at 11:56 pm

previewing Y! App. Platform small view content

leave a comment »

<!--
usage:
- upload this file to a server
- create yap app
- set app url to the location of this file and preview
- click the 'load' link to load 'preview' div w/ small view code
-->
<div style="margin:20px">
<div id="preview" style="border: 1px dashed #bbbbbb; width: 300px; margin-bottom: 20px;">preview</div>
load</div>
<yml:a view="YahooSmallView" insert="preview">load</yml:a>

Written by Erik

December 19, 2008 at 11:36 pm

Posted in code

Tagged with , , , ,

notes from douglas crockford’s 12/17/08 talk on js performance

leave a comment »

Note: all credit for this material goes to Douglas Crockford, though nothing noted here is a direct quote.

  • don’t optimize w/out measuring
  • don’t optimize something that isn’t performed > once
  • optimize when we cross line of slow and/or failing performance
    • slow apps cause irritation
    • failing apps cause desertion
  • O(n^2) lines do not reflect optimizations dramatically
    • general rule: make n small
  • use ajax for jit data delivery
  • dyn el generation is faster than data fetch, so cache data, not markup
  • we can’t know what will take time before we start working, so test constantly to get preview of performance issues
  • Best practice for pagination?  Request data in chunks, ie let server handle pagination
  • how to speed up graphics rendering?  We’re unlikely to get performance boost for in JS layer, so look to browser-level support
  • can we speed up rendering & layout?
    • simplify css & html, but otherwise no.
    • we currently lack performance tools for measuring dom manipulation performance metrics
    • favor better ajax design over js optimization
    • reduce browser rendering by leveraging ajax interactivity, ie jit/on-demand page construction

Written by Erik

December 19, 2008 at 11:28 pm

Posted in notes

Tagged with , ,