Erik's blog

Code, notes, recipes, general musings

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 , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: