HTTP API .getBulkRequest正确回复

时间:2017-12-27 10:12:58

标签: php curl matomo

希望有人能用以下代码解释问题。我有两个URL通过API.getBulkRequest

从自托管的Piwik设置传递数据

问题是来自每个URL的数据被分成两行表,而print表中的数据用作一个表。这张图中清楚地显示了这一点。

Table of returned data

如何在表格的一行中print数据?完整代码很长但很容易理解。

<?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的大力支持,包括圣诞节让我走到这一步。我只是遇到了一些远远超出我头脑的问题。没有他们的帮助,我仍然会在一个洞穴中狩猎Woolly Mammoth。

1 个答案:

答案 0 :(得分:1)

如上所述总结

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