Skip to content

Instantly share code, notes, and snippets.

@neopunisher
Forked from hubgit/curl-multi.php
Created June 25, 2010 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neopunisher/453023 to your computer and use it in GitHub Desktop.
Save neopunisher/453023 to your computer and use it in GitHub Desktop.
<?php
define('EMAIL', 'test@example.com'); // edit this
define('FLICKR_API_KEY', ''); // edit this
define('RAPLEAF_API_KEY', ''); // edit this
define('USER_AGENT', 'example user agent (http://www.example.com)'); // edit this
define('COOKIE_FILE', '/tmp/cookies.txt'); // edit this if needed
$default_curl_params = array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_MAXREDIRS => 5,
CURLOPT_USERAGENT => USER_AGENT,
CURLOPT_COOKIEJAR => COOKIE_FILE,
CURLOPT_COOKIEFILE => COOKIE_FILE,
CURLOPT_TIMEOUT => 10,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_VERBOSE => TRUE,
);
$sites = array(
'flickr' => array(
CURLOPT_URL => 'http://api.flickr.com/services/rest/',
CURLOPT_HTTPGET => TRUE,
CURLOPT_POST => FALSE,
CURLOPT_POSTFIELDS => array(
'api_key' => FLICKR_API_KEY,
'method' => 'flickr.people.findByEmail',
'format' => 'json',
'find_email' => EMAIL,
),
),
'rapleaf' => array(
CURLOPT_URL => 'http://api.rapleaf.com/v3/person/email/' . EMAIL,
CURLOPT_HTTPGET => TRUE,
CURLOPT_POST => FALSE,
CURLOPT_POSTFIELDS => array(
'api_key' => RAPLEAF_API_KEY,
),
),
);
$curl = curl_multi_init();
$connections = array();
foreach ($sites as $name => $params){
foreach ($default_curl_params as $key => $value)
$params[$key] = $value; // can't use array_merge as CURL_* keys are numeric
// convert POSTFIELDS to a query string if using HTTP GET
if ($params[CURLOPT_HTTPGET] && !empty($params[CURLOPT_POSTFIELDS])){
$params[CURLOPT_URL] .= '?' . http_build_query($params[CURLOPT_POSTFIELDS]);
unset($params[CURLOPT_POSTFIELDS]);
}
$connections[$name] = curl_init();
curl_setopt_array($connections[$name], $params);
curl_multi_add_handle($curl, $connections[$name]);
}
do { $mrc = curl_multi_exec($curl, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK)
if (curl_multi_select($curl) != -1)
do { $mrc = curl_multi_exec($curl, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM);
$output = array();
foreach ($connections as $key => $connection){
$output[$key] = curl_multi_getcontent($connection);
curl_multi_remove_handle($curl, $connection);
}
curl_multi_close($curl);
print_r($output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment