(jump to the practical example)
PHP has a set of cURL functions to let your script download other
webpages. If you use cURL to scrape data or
build mashups,
you may need to fetch more than one page. This could create a massive performance problem, adding seconds to your
own script's runtime because you have to wait for several individual cURL requests to come back.
Enter curl_multi_init. This family of functions
allows you to combine cURL handles and execute them simultaneously.
// this example does NOT use simultaneous requests, it must wait for each response
// request 1
$ch = curl_init('http://webservice.one.com/'); // initialize the request
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // store the page contents
$response_1 = curl_exec($ch); // actually make the request
// request 2
$ch = curl_init('http://webservice.two.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_2 = curl_exec($ch);
// normally you would process your results here
echo "$response_1 $response_2";
// with curl_multi, you only have to wait for the longest-running request
// build the individual requests as above, but do not execute them
$ch_1 = curl_init('http://webservice.one.com/');
$ch_2 = curl_init('http://webservice.two.com/');
curl_setopt($ch_1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_2, CURLOPT_RETURNTRANSFER, true);
// build the multi-curl handle, adding both $ch
$mh = curl_multi_init();
curl_multi_add_handle($mh, $ch_1);
curl_multi_add_handle($mh, $ch_2);
// execute all queries simultaneously, and continue when all are complete
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running);
// all of our requests are done, we can now access the results
$response_1 = curl_multi_getcontent($ch_1);
$response_2 = curl_multi_getcontent($ch_2);
echo "$response_1 $response_2"; // same output as first example
If both websites take one second to return, we literally cut our page load time in half by using the second example instead of the first. Sweet!
Here's an example where we run multiple Twitter searches and combine the results to display them on our own site.
As a bonus, it also caches the results for 1 minute so we avoid hitting Twitter's rate limit if we get
a ton of visitors at the same time. You can change $minutes to any number you feel
comfortable with, but it's important to include because you will end up with a complete blank
list if your page gets a lot of hits, which is precisely the worst time to kill your content.
function tweets() {
// check cache
$cache = 'twitter-search.txt';
if (file_exists($cache)) {
clearstatcache();
$minutes = 1; // how long to wait before refreshing the cache
if (filemtime($cache) > (time() - (60 * $minutes)) {
return file_get_contents($cache);
}
}
// we are going to search for tweets mentioning these keywords
$keywords = array(
'javascript',
'html5',
'css3'
);
// build the requests
$ch = array();
$mh = curl_multi_init();
for ($i = 0; $i < count($keywords); $i++) {
$keyword = $keywords[$i];
$ch[$i] = curl_init();
curl_setopt($ch[$i], CURLOPT_URL,
'http://search.twitter.com/search.json?rpp=3&q=' . $keyword);
curl_setopt($ch[$i], CURLOPT_USERAGENT,
'Twitter requires you to set a user agent, any value works here.');
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch[$i], CURLOPT_HEADER, false);
curl_multi_add_handle($mh, $ch[$i]);
}
// execute the requests simultaneously
$running = 0;
do {
curl_multi_exec($mh, $running);
} while ($running > 0);
// display the results
$output = '';
for ($i = 0; $i < count($keywords); $i++) {
// $results contains this keyword's tweets as an associative array
$results = reset(json_decode(curl_multi_getcontent($ch[$i]), true));
$resultCount = count($results);
// link to our keyword
$output .= '<dl><dt><a href="http://search.twitter.com/search?' . $keywords[$i] . '</a></dt>';
// dump the search results
for ($j = 0; $j < $resultCount; $j++) {
$id = $results[$j]['id']; // twitter user ID
$user = $results[$j]['from_user']; // twitter user name
$tweet = $results[$j]['text']; // tweet text
$url = "http://www.twitter.com/$user/status/$id/"; // link to the tweet
$output .= '<a href="' . $url . '">' . $tweet . ' — ' . $user . '</a>';
}
$output .= '</dd></dl>';
}
file_put_contents($cache, $output); // store in local cache for performance boost
return $output;
}
echo tweets();
I'm a Front-End Engineer at Yahoo! working on the Mail and Messenger teams. I blog about web design and development topics including accessibility, usability, performance, and developing HTML / CSS / JavaScript applications on Appcelerator Titanium and Adobe AIR.
If you're a web developer, you might enjoy Jelo, my JavaScript library.
A few panoramic shots I took at SDCC 2010. #geek http://bit.ly/bwX6GB
JS version of Regex prime number checker:
function isPrime(n) {
return Array(n + 1).join("1")
.search(/^1?$|^(11+?)\1+$/) == -1;
}
Погрузился в пучину EcmaScript5, местами увлекательно, местами нудно =)
Modernizr http://ow.ly/18njQ1
A Collection of 20 HTML5 Video Players - a round-up of JavaScript and html5 alternatives to Flash-based media player... http://ow.ly/18njQ2
jQuery TOOLS - The missing UI library for the Web http://ow.ly/18njQ3
Contactable - A jQuery Plugin | the odin http://ow.ly/18njQ4
Giants vs Dodgers, sweet seats. http://twitpic.com/2ag9pa
@snookca That'll be fixed next week. I promise.
@snookca I was tryna not name names ;) But really that was just par for the course today, pretty hectic day. As I'm sure you know.
Who breaks major stuff after 4pm on Friday? On the last day of the sprint, no less. Tsk. (wasn't me)
Awesome live git tracker for teams: http://www.utsup.com/
RT @DerrenBrown: Blog post: Camera Software Lets You See Into the Past http://bit.ly/9kjVg5
10 invites to the new version of Digg: http://bit.ly/dqM8EV
Threaded vs Evented Servers, great look at the whats and whys. http://bit.ly/bDUEjn #geek
Nav, Context menus, "app-style" toolbars in sample chapter http://bit.ly/csTRY8 of new YUI book http://bit.ly/cJINoV
Add a side-mounted End Call button to your iPhone 4: http://bit.ly/cGxPBD #funny #geekAll original work on this site is covered by a Creative Commons Attribution 3.0 license unless otherwise specified.
You may share or use any code or images from this site in any manner, for free, so long as reasonable effort has been made to give credit where due.
The views expressed in the posts and comments on this blog do not necessarily reflect the views of Yahoo!