HTTP API .getBulkRequest 正确响应
HTTP API .getBulkRequest responding correctly
希望有人能解释下面代码的问题。我有两个 URLs 通过 API.getBulkRequest
的自托管 Piwik 设置传递数据
问题是来自每个 URL 的数据被拆分成 table 的两行,而它是 print
成一个 table 用作一个。这张图片清楚地显示了这一点。
我怎样才能 print
跨越一行 table 的数据?完整代码很长但很容易理解。
<?php
function curl( $url=NULL, $options=NULL ){
$cacert='c:/wwwroot/cacert.pem';
$vbh = fopen('php://temp', 'w+');
$res=array(
'response' => NULL,
'info' => array( 'http_code' => 100 ),
'headers' => NULL,
'errors' => NULL
);
if( is_null( $url ) ) return (object)$res;
$curl=curl_init();
if( parse_url( $url,PHP_URL_SCHEME )=='http' ){
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $curl, CURLOPT_CAINFO, $cacert );
}
curl_setopt( $curl, CURLOPT_URL,trim( $url ) );
curl_setopt( $curl, CURLOPT_AUTOREFERER, true );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $curl, CURLOPT_FAILONERROR, true );
curl_setopt( $curl, CURLOPT_HEADER, false );
curl_setopt( $curl, CURLINFO_HEADER_OUT, false );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_BINARYTRANSFER, true );
curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 20 );
curl_setopt( $curl, CURLOPT_TIMEOUT, 60 );
curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36' );
curl_setopt( $curl, CURLOPT_MAXREDIRS, 10 );
curl_setopt( $curl, CURLOPT_ENCODING, '' );
curl_setopt( $curl,CURLOPT_VERBOSE,true );
curl_setopt( $curl,CURLOPT_NOPROGRESS,true );
curl_setopt( $curl,CURLOPT_STDERR,$vbh );
if( isset( $options ) && is_array( $options ) ){
foreach( $options as $param => $value ) curl_setopt( $curl, $param, $value );
}
$res=(object)array(
'response' => curl_exec( $curl ),
'info' => (object)curl_getinfo( $curl ),
'errors' => curl_error( $curl )
);
rewind( $vbh );
$res->verbose=stream_get_contents( $vbh );
fclose( $vbh );
curl_close( $curl );
return $res;
}
$url='https://demo.piwik.org/';
$params=array(
'module' => 'API',
'method' => 'API.getBulkRequest',
'date' => 'last3',
'period' => 'month',
'idSite' => 7,
'format' => 'json',
'token_auth' => 'anonymous'
);
$urls=array(
array( 'method' => 'Actions.get' ),
array( 'method' => 'VisitsSummary.get' )
);
$tmp=array();
foreach( $urls as $index => $site ) $tmp[]='urls['.$index.']='.urlencode( http_build_query( $site ) );
$options=array(
CURLOPT_POSTFIELDS => http_build_query( $params ) . '&' . implode('&',$tmp),
CURLOPT_POST => true
);
$result = curl( $url, $options );
if( $result->info->http_code==200 ){
$data=json_decode( $result->response );
$reqd=array('nb_pageviews','nb_uniq_pageviews','nb_outlinks','bounce_count','bounce_rate','avg_time_on_site');
$methods=array('Actions','VisitsSummary');
$output=array();
foreach( $data as $index => $obj ){
$keys = array_keys( get_object_vars( $obj ) );
$tmp = array();
foreach( $keys as $i => $key ){
if( !empty( $obj->{$key} ) ) {
foreach( $reqd as $item ){
if( property_exists( $obj->{$key}, $item ) ) $tmp[ $item ]=$obj->{$key}->$item;
}
}
}
$output[ $methods[ $index ] ][ $key ]=$tmp;
}
foreach( $output as $index => $action ){
$months = array_keys( $action );
foreach( $months as $month ){
$nb_pageviews = array_key_exists('nb_pageviews',$action[ $month ] ) ? $action[ $month ]['nb_pageviews'] : 0;
$nb_uniq_pageviews = array_key_exists('nb_uniq_pageviews', $action[ $month ] ) ? $action[ $month ]['nb_uniq_pageviews'] : 0;
$nb_outlinks = array_key_exists('nb_outlinks',$action[ $month ] ) ? $action[ $month ]['nb_outlinks'] : 0;
$bounce_count = array_key_exists('bounce_count',$action[ $month ] ) ? $action[ $month ]['bounce_count'] : 0;
$bounce_rate = array_key_exists('bounce_rate',$action[ $month ] ) ? $action[ $month ]['bounce_rate'] : 0;
$avg_time_on_site = array_key_exists('avg_time_on_site',$action[ $month ] ) ? $action[ $month ]['avg_time_on_site'] : 0;
$action_percent = get_percentage( $nb_pageviews, $nb_outlinks );
$unique_percent = get_percentage( $nb_pageviews, $nb_uniq_pageviews );
print("<tr>");
print("<td class='month subscriber subscriber-fixed-alone fixed-cell'>" .date('F Y',strtotime($month)). "</td>");
print("<td class='stat number text-center'>$nb_pageviews</td>");
print("<td class='stat number text-center'>$nb_uniq_pageviews ($unique_percent%)</td>");
print("<td class='stat number text-center'>$nb_outlinks ($action_percent%)</td>");
print("<td class='stat number text-center'>$bounce_count ($bounce_rate)</td>");
print("<td class='stat number text-center'>$avg_time_on_site Secs</td>");
print("</tr>");
}
}
} else {
echo "Error: {$result->info->http_code}";
} ?>
出处: 非常感谢 @RamRaider 的大力支持,包括在圣诞节那天让我走到这一步。我只是有几个问题远远超出了我的头脑。没有他们的帮助,我仍然会在洞穴中狩猎猛犸象。
根据讨论进行总结
foreach( $data as $index => $obj ){
$keys = array_keys( get_object_vars( $obj ) );
$tmp = array();
foreach( $keys as $i => $key ){
try{
if( isset( $obj->{$key} ) ) {
foreach( $reqd as $item ){
if( is_object( $obj->{$key} ) && property_exists( $obj->{$key}, $item ) ) {
$tmp[ $item ]=$obj->{$key}->$item;
} else {/* keep empty months */
$output[ $methods[ $index ] ][ $key ]=$key;
}
}
}
}catch( Exception $e ){
echo $e->getMessage();
continue;
}
}
$output[ $methods[ $index ] ][ $key ]=$tmp;
}
/* merge action results and sort */
$output=array_merge_recursive( $output[ $methods[0] ], $output[ $methods[1] ] );
krsort( $output );
foreach( $output as $month => $data ){
$nb_pageviews = array_key_exists('nb_pageviews',$data ) ? $data['nb_pageviews'] : 0;
$nb_uniq_pageviews = array_key_exists('nb_uniq_pageviews', $data ) ? $data['nb_uniq_pageviews'] : 0;
$nb_outlinks = array_key_exists('nb_outlinks',$data ) ? $data['nb_outlinks'] : 0;
$bounce_count = array_key_exists('bounce_count',$data ) ? $data['bounce_count'] : 0;
$bounce_rate = array_key_exists('bounce_rate',$data ) ? $data['bounce_rate'] : 0;
$avg_time_on_site = array_key_exists('avg_time_on_site',$data ) ? $data['avg_time_on_site'] : 0;
$action_percent = get_percentage( $nb_pageviews, $nb_outlinks );
$unique_percent = get_percentage( $nb_pageviews, $nb_uniq_pageviews );
print("<tr>");
print("<td class='month subscriber subscriber-fixed-alone fixed-cell'>" .date('F Y',strtotime($month)). "</td>");
print("<td class='stat number text-center'>$nb_pageviews</td>");
print("<td class='stat number text-center'>$nb_uniq_pageviews ($unique_percent%)</td>");
print("<td class='stat number text-center'>$nb_outlinks ($action_percent%)</td>");
print("<td class='stat number text-center'>$bounce_count ($bounce_rate)</td>");
print("<td class='stat number text-center'>$avg_time_on_site Secs</td>");
print("</tr>");
}
希望有人能解释下面代码的问题。我有两个 URLs 通过 API.getBulkRequest
的自托管 Piwik 设置传递数据问题是来自每个 URL 的数据被拆分成 table 的两行,而它是 print
成一个 table 用作一个。这张图片清楚地显示了这一点。
我怎样才能 print
跨越一行 table 的数据?完整代码很长但很容易理解。
<?php
function curl( $url=NULL, $options=NULL ){
$cacert='c:/wwwroot/cacert.pem';
$vbh = fopen('php://temp', 'w+');
$res=array(
'response' => NULL,
'info' => array( 'http_code' => 100 ),
'headers' => NULL,
'errors' => NULL
);
if( is_null( $url ) ) return (object)$res;
$curl=curl_init();
if( parse_url( $url,PHP_URL_SCHEME )=='http' ){
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $curl, CURLOPT_CAINFO, $cacert );
}
curl_setopt( $curl, CURLOPT_URL,trim( $url ) );
curl_setopt( $curl, CURLOPT_AUTOREFERER, true );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $curl, CURLOPT_FAILONERROR, true );
curl_setopt( $curl, CURLOPT_HEADER, false );
curl_setopt( $curl, CURLINFO_HEADER_OUT, false );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_BINARYTRANSFER, true );
curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 20 );
curl_setopt( $curl, CURLOPT_TIMEOUT, 60 );
curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36' );
curl_setopt( $curl, CURLOPT_MAXREDIRS, 10 );
curl_setopt( $curl, CURLOPT_ENCODING, '' );
curl_setopt( $curl,CURLOPT_VERBOSE,true );
curl_setopt( $curl,CURLOPT_NOPROGRESS,true );
curl_setopt( $curl,CURLOPT_STDERR,$vbh );
if( isset( $options ) && is_array( $options ) ){
foreach( $options as $param => $value ) curl_setopt( $curl, $param, $value );
}
$res=(object)array(
'response' => curl_exec( $curl ),
'info' => (object)curl_getinfo( $curl ),
'errors' => curl_error( $curl )
);
rewind( $vbh );
$res->verbose=stream_get_contents( $vbh );
fclose( $vbh );
curl_close( $curl );
return $res;
}
$url='https://demo.piwik.org/';
$params=array(
'module' => 'API',
'method' => 'API.getBulkRequest',
'date' => 'last3',
'period' => 'month',
'idSite' => 7,
'format' => 'json',
'token_auth' => 'anonymous'
);
$urls=array(
array( 'method' => 'Actions.get' ),
array( 'method' => 'VisitsSummary.get' )
);
$tmp=array();
foreach( $urls as $index => $site ) $tmp[]='urls['.$index.']='.urlencode( http_build_query( $site ) );
$options=array(
CURLOPT_POSTFIELDS => http_build_query( $params ) . '&' . implode('&',$tmp),
CURLOPT_POST => true
);
$result = curl( $url, $options );
if( $result->info->http_code==200 ){
$data=json_decode( $result->response );
$reqd=array('nb_pageviews','nb_uniq_pageviews','nb_outlinks','bounce_count','bounce_rate','avg_time_on_site');
$methods=array('Actions','VisitsSummary');
$output=array();
foreach( $data as $index => $obj ){
$keys = array_keys( get_object_vars( $obj ) );
$tmp = array();
foreach( $keys as $i => $key ){
if( !empty( $obj->{$key} ) ) {
foreach( $reqd as $item ){
if( property_exists( $obj->{$key}, $item ) ) $tmp[ $item ]=$obj->{$key}->$item;
}
}
}
$output[ $methods[ $index ] ][ $key ]=$tmp;
}
foreach( $output as $index => $action ){
$months = array_keys( $action );
foreach( $months as $month ){
$nb_pageviews = array_key_exists('nb_pageviews',$action[ $month ] ) ? $action[ $month ]['nb_pageviews'] : 0;
$nb_uniq_pageviews = array_key_exists('nb_uniq_pageviews', $action[ $month ] ) ? $action[ $month ]['nb_uniq_pageviews'] : 0;
$nb_outlinks = array_key_exists('nb_outlinks',$action[ $month ] ) ? $action[ $month ]['nb_outlinks'] : 0;
$bounce_count = array_key_exists('bounce_count',$action[ $month ] ) ? $action[ $month ]['bounce_count'] : 0;
$bounce_rate = array_key_exists('bounce_rate',$action[ $month ] ) ? $action[ $month ]['bounce_rate'] : 0;
$avg_time_on_site = array_key_exists('avg_time_on_site',$action[ $month ] ) ? $action[ $month ]['avg_time_on_site'] : 0;
$action_percent = get_percentage( $nb_pageviews, $nb_outlinks );
$unique_percent = get_percentage( $nb_pageviews, $nb_uniq_pageviews );
print("<tr>");
print("<td class='month subscriber subscriber-fixed-alone fixed-cell'>" .date('F Y',strtotime($month)). "</td>");
print("<td class='stat number text-center'>$nb_pageviews</td>");
print("<td class='stat number text-center'>$nb_uniq_pageviews ($unique_percent%)</td>");
print("<td class='stat number text-center'>$nb_outlinks ($action_percent%)</td>");
print("<td class='stat number text-center'>$bounce_count ($bounce_rate)</td>");
print("<td class='stat number text-center'>$avg_time_on_site Secs</td>");
print("</tr>");
}
}
} else {
echo "Error: {$result->info->http_code}";
} ?>
出处: 非常感谢 @RamRaider 的大力支持,包括在圣诞节那天让我走到这一步。我只是有几个问题远远超出了我的头脑。没有他们的帮助,我仍然会在洞穴中狩猎猛犸象。
根据讨论进行总结
foreach( $data as $index => $obj ){
$keys = array_keys( get_object_vars( $obj ) );
$tmp = array();
foreach( $keys as $i => $key ){
try{
if( isset( $obj->{$key} ) ) {
foreach( $reqd as $item ){
if( is_object( $obj->{$key} ) && property_exists( $obj->{$key}, $item ) ) {
$tmp[ $item ]=$obj->{$key}->$item;
} else {/* keep empty months */
$output[ $methods[ $index ] ][ $key ]=$key;
}
}
}
}catch( Exception $e ){
echo $e->getMessage();
continue;
}
}
$output[ $methods[ $index ] ][ $key ]=$tmp;
}
/* merge action results and sort */
$output=array_merge_recursive( $output[ $methods[0] ], $output[ $methods[1] ] );
krsort( $output );
foreach( $output as $month => $data ){
$nb_pageviews = array_key_exists('nb_pageviews',$data ) ? $data['nb_pageviews'] : 0;
$nb_uniq_pageviews = array_key_exists('nb_uniq_pageviews', $data ) ? $data['nb_uniq_pageviews'] : 0;
$nb_outlinks = array_key_exists('nb_outlinks',$data ) ? $data['nb_outlinks'] : 0;
$bounce_count = array_key_exists('bounce_count',$data ) ? $data['bounce_count'] : 0;
$bounce_rate = array_key_exists('bounce_rate',$data ) ? $data['bounce_rate'] : 0;
$avg_time_on_site = array_key_exists('avg_time_on_site',$data ) ? $data['avg_time_on_site'] : 0;
$action_percent = get_percentage( $nb_pageviews, $nb_outlinks );
$unique_percent = get_percentage( $nb_pageviews, $nb_uniq_pageviews );
print("<tr>");
print("<td class='month subscriber subscriber-fixed-alone fixed-cell'>" .date('F Y',strtotime($month)). "</td>");
print("<td class='stat number text-center'>$nb_pageviews</td>");
print("<td class='stat number text-center'>$nb_uniq_pageviews ($unique_percent%)</td>");
print("<td class='stat number text-center'>$nb_outlinks ($action_percent%)</td>");
print("<td class='stat number text-center'>$bounce_count ($bounce_rate)</td>");
print("<td class='stat number text-center'>$avg_time_on_site Secs</td>");
print("</tr>");
}