Php HTTP API.getBulkRequest正确响应

Php HTTP API.getBulkRequest正确响应,php,curl,matomo,Php,Curl,Matomo,希望有人能用下面的代码解释一个问题。我有两个URL通过API.getBulkRequest从自托管Piwik设置传递数据 问题在于,来自每个URL的数据被分割到一个表的两行中,而它被作为一行打印到一个表中。这在这张图中清楚地显示出来 如何跨表的一行打印数据?完整的代码很长,但很容易理解 <?php function curl( $url=NULL, $options=NULL ){ $cacert='c:/wwwroot/cacert.pem'

希望有人能用下面的代码解释一个问题。我有两个URL通过API.getBulkRequest从自托管Piwik设置传递数据

问题在于,来自每个URL的数据被分割到一个表的两行中,而它被作为一行打印到一个表中。这在这张图中清楚地显示出来

如何跨表的一行打印数据?完整的代码很长,但很容易理解

<?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}";              
 } ?>

如前所述进行总结

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>"); 
}
foreach($index=>$obj){
$keys=数组\键(获取\对象\变量($obj));
$tmp=array();
foreach($i=>$key的键){
试一试{
if(isset($obj->{$key})){
foreach($项目需求){
如果(是_对象($obj->{$key})和&property_存在($obj->{$key},$item)){
$tmp[$item]=$obj->{$key}->$item;
}否则{/*保留空月份*/
$output[$methods[$index][$key]=$key;
}
}
}
}捕获(例外$e){
echo$e->getMessage();
持续
}
}
$output[$methods[$index][$key]=$tmp;
}
/*合并操作结果并排序*/
$output=array\u merge\u recursive($output[$methods[0]],$output[$methods[1]]);
krsort($输出);
foreach($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=数组_key_存在('nb_outlinks',$data)$data['nb_outlinks']:0;
$bounce\u count=数组\u键\u存在($bounce\u count',$data)?$data['bounce\u count']:0;
$bounce_rate=数组_key_存在($bounce_rate',$data)?$data['bounce_rate']:0;
$avg_time_on_site=数组_key_存在($avg_time_on_site',$data)$data['avg_time_on_site']:0;
$action\u percent=get\u percentage($nb\u页面浏览量,$nb\u大纲链接);
$unique\u percent=get\u percentage($nb\u页面浏览量,$nb\u uniq\u页面浏览量);
打印(“”);
打印(“.date('fy',strottime($month))。”;
打印($nb_页面浏览量);
打印($nb_uniq_页面浏览量($unique_%)));
打印($nb\u大纲($action\u percent%));
打印($bounce\U count($bounce\U rate)”);
打印($avg_time_on_site Secs);
打印(“”);
}