<?php
function parse( $limiter = null ) {
/**
* Start parsing!
*/
$return_values = curl_multi_download( array(
"users/" . userid,
'projects',
'comments/?o=-submit_date&page_size=5',
'tasks/?deadline=5',
), urlpath );
/**
* End parsing!
*/
switch ( $limiter ) {
case 'index':
$return = array( 'projects' => $return_values['projects'],
'username' => $return_values['users/' . userid]['username'],
'calander_num' => '2',
'alerts_num' => '32',
'messages_num' => '123',
'7_days_tasks_due' => '10'
);
break;
case 'dashboard':
$return = array( 'username' => $return_values['users/' . userid]['username'],
'recent_comments' => $return_values['comments/?o=-submit_date&page_size=5'],
't_7_days' => day_counter($return_values['tasks/?deadline=5'], 7),
'projects' => $return_values['projects']
);
break;
default:
return null;
break;
}
return $return;
}
function day_counter($array, $index) {
$datearray = array();
foreach ($array['results'] as $key => &$value) {
array_push($datearray , $array['results'][$key]['deadline']);
}
$datearray = array_count_values($datearray);
for ($i = 0; $i < $index; $i++) {
if (isset($datearray[date('Y-m-d', strtotime('+'.$i.' day', time()))])) {
$return[$i] = $datearray[date('Y-m-d', strtotime('+'.$i.' day', time()))];
} else {
$return[$i] = 0;
}
}
return $return;
}
function curl_multi_download( array $urls, $urlpath, array $custom_options = array() )
{
// make sure the rolling window isn't greater than the # of urls
$rolling_window = 20;
$rolling_window = ( sizeof( $urls ) < $rolling_window ) ? sizeof( $urls ) : $rolling_window;
$master = curl_multi_init();
$curl_arr = array();
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
) + $custom_options;
// start the first batch of requests
for ( $i = 0; $i < $rolling_window; $i++ )
{
$ch = curl_init();
$options[CURLOPT_URL] = $urlpath . $urls[$i];
curl_setopt_array( $ch, $options );
curl_multi_add_handle( $master, $ch );
}
$return_values = array();
do
{
while ( ( $execrun = curl_multi_exec( $master, $running ) ) == CURLM_CALL_MULTI_PERFORM );
if ( $execrun != CURLM_OK )
break;
// a request was just completed -- find out which one
while ( $done = curl_multi_info_read( $master ) )
{
$info = curl_getinfo( $done['handle'] );
// request successful. process output using the callback function.
$output = curl_multi_getcontent( $done['handle'] );
// call_user_func_array($callback, array($info, $output));
if ( $info['http_code'] == '200' ) {
//print_r(curl_getinfo($done['handle']));
//$return_values[curl_getinfo($done['handle'])['url']] = json_decode( $output, true );
$return_values[rtrim( substr( curl_getinfo($done['handle'])['url'], strlen ($urlpath) ), '/' )] = json_decode( $output, true );
}
// var_dump($return_values);
if ( isset( $urls[$i + 1] ) )
{
// start a new request (it's important to do this before removing the old one)
$ch = curl_init();
$options[CURLOPT_URL] = $urlpath . $urls[$i++]; // increment i
curl_setopt_array( $ch, $options );
curl_multi_add_handle( $master, $ch );
}
// remove the curl handle that just completed
curl_multi_remove_handle( $master, $done['handle'] );
}
} while ( $running );
curl_multi_close( $master );
return $return_values;
}
preferences:
38.33 ms | 402 KiB | 5 Q