在 PHP 中传递多个 HTTP API URL

Passing multiple HTTP API URL in PHP

我需要从 Piwik Analytics 调用两个或多个 HTTP APIGET META。不幸的是,他们的 method=API.getBulkRequest 方法不适用于 PHP 调用,并且仅适用于 JSON 和 XML - 两者都不适用于此请求。

是否可以合并

https://demo.piwik.org/?module=API&method=Actions.get&idSite=7&period=month&date=last3&format=xml&token_auth=anonymous

https://demo.piwik.org/?module=API&method=VisitsSummary.get&idSite=7&period=month&date=last3&format=xml&token_auth=anonymous

使用此方法调用API

$url = "https://demo.piwik.org/";
$url .= "?module=API&method=Actions.get";
$url .= "&idSite=7&period=month&date=last3";
$url .= "&format=php&filter_sort_order=desc";
$url .= "&token_auth=anonymous";

所以我可以 GET 使用

的数据
foreach ($content as $dates => $row) {
    $visits = @$row['nb_pageviews'] ?: 0;
    $unique = @$row['nb_uniq_pageviews'] ?: 0;
    $actions = @$row['nb_outlinks'] ?: 0;
    $bounce = @$row['bounce_count'] ?: 0;
    $bounce_rate = @$row['bounce_rate'] ?: 0;
    $time = @$row['avg_time_on_site'] ?: 0;

感谢您的帮助,祝明天圣诞快乐

更新 .PHP 文件中的完整代码

<?php
            $url = "https://demo.piwik.org/";
            $url .= "?module=API&method=Actions.get";
            $url .= "&idSite=7&period=month&date=last3";
            $url .= "&format=php&filter_sort_order=desc";
            $url .= "&token_auth=anonymous";

            $fetched = file_get_contents($url);
            $content = unserialize($fetched);
            krsort($content);

            // case error
            if (!$content) {
                print("<tr><td class='month subscriber subscriber-fixed-alone fixed-cell'></td><td class='stat number text-center'>Error, analytics not available at the moment.</td></tr>");
            }

            foreach ($content as $dates => $row) {
                $visits = @$row['nb_pageviews'] ?: 0;
                $unique = @$row['nb_uniq_pageviews'] ?: 0;
                $actions = @$row['nb_outlinks'] ?: 0;
                $bounce = @$row['bounce_count'] ?: 0;
                $bounce_rate = @$row['bounce_rate'] ?: 0;
                $time = @$row['avg_time_on_site'] ?: 0;

                $date = $dates;
                $action_percent = get_percentage($visits,$actions);
                $unique_percent = get_percentage($visits,$unique);

                print("<tr>");
                print("<td class='month subscriber subscriber-fixed-alone fixed-cell'>" .date('F Y',strtotime($date)). "</td>");
                print("<td class='stat number text-center'>$visits</td>");
                print("<td class='stat number text-center'>$unique ($unique_percent%)</td>");
                print("<td class='stat number text-center'>$actions ($action_percent%)</td>");
                print("<td class='stat number text-center'>$bounce ($bounce_rate)</td>");
                print("<td class='stat number text-center'>$time</td>");
                print("</tr>");
            }?>

莫因, 你不能使用 Piwik 的批量请求功能吗API: 请参阅 https://developer.piwik.org/api-reference/reporting-api(高级用户:一次发送多个 API 请求)?

此外,我建议使用 HTTP 客户端库,例如 https://packagist.org/packages/guzzlehttp/guzzle,它有一组非常好的功能来促进服务器端 HTTP 调用(例如同步和异步模式)

为了使用 getBulkRequest(我无法在他们的网站上找到详细信息)甚至其他 API 调用,您可能会发现使用 curl 更容易,因为您可以快速定制通过更改传递给下面的 curl 函数的几个选项并构建不同的 $params 有效负载来请求。

function curl( $url=NULL, $options=NULL ){
    /*
        Download a copy of cacert.pem from
        https://curl.haxx.se/docs/caextract.html

        and then edit below as appropriate
    */
    $cacert='c:/wwwroot/cacert.pem';

    /* for advanced debugging info */
    $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;

    session_write_close();

    /* Initialise curl request object */
    $curl=curl_init();
    if( parse_url( $url,PHP_URL_SCHEME )=='https' ){
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
        curl_setopt( $curl, CURLOPT_CAINFO, $cacert );
    }

    /* Define standard options */
    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, '' );

    /* advanced debugging info */
    curl_setopt( $curl,CURLOPT_VERBOSE,true );
    curl_setopt( $curl,CURLOPT_NOPROGRESS,true );
    curl_setopt( $curl,CURLOPT_STDERR,$vbh );

    /* Assign runtime parameters as options */
    if( isset( $options ) && is_array( $options ) ){
        foreach( $options as $param => $value ) curl_setopt( $curl, $param, $value );
    }

    /* Execute the request and store responses */
    $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;
}

/*
The original url
----------------
$url = "https://demo.piwik.org/";
$url .= "?module=API&method=Actions.get";
$url .= "&idSite=7&period=month&date=last3";
$url .= "&format=php&filter_sort_order=desc";
$url .= "&token_auth=anonymous";
*/

$url='https://demo.piwik.org/';

/* original url as array of parameters to POST */
$params=array(
    'module'            =>  'API',
    'method'            =>  'Actions.get',
    'idSite'            =>  7,
    'period'            =>  'month',
    'date'              =>  'last3',
    'format'            =>  'php',
    'filter_sort_order' =>  'desc',
    'token_auth'        =>  'anonymous'

);
/* Set the options for POSTing the data */
$options=array(
    CURLOPT_POSTFIELDS  =>  http_build_query( $params ),
    CURLOPT_POST        =>  true
);
/* Make the request */
$result = curl( $url, $options );

/* If the response status code is 200 (OK) do stuff with data */
if( $result->info->http_code==200 ){
    $data=unserialize( $result->response );
    echo '<pre>',print_r($data,true),'</pre>';
} else {
    /* panic */
    echo "Error: {$result->info->http_code}";
}

以上将输出如下:

Array
(
    [2017-10] => Array
        (
            [nb_pageviews] => 37849
            [nb_uniq_pageviews] => 30775
            [nb_downloads] => 54
            [nb_uniq_downloads] => 50
            [nb_outlinks] => 1911
            [nb_uniq_outlinks] => 1808
            [nb_searches] => 1452
            [nb_keywords] => 925
            [avg_time_generation] => 0.672
        )

    [2017-11] => Array
        (
            [nb_pageviews] => 36409
            [nb_uniq_pageviews] => 29518
            [nb_downloads] => 50
            [nb_uniq_downloads] => 48
            [nb_outlinks] => 1841
            [nb_uniq_outlinks] => 1724
            [nb_searches] => 1238
            [nb_keywords] => 775
            [avg_time_generation] => 0.672
        )

    [2017-12] => Array
        (
            [nb_pageviews] => 28945
            [nb_uniq_pageviews] => 21623
            [nb_downloads] => 34
            [nb_uniq_downloads] => 28
            [nb_outlinks] => 1343
            [nb_uniq_outlinks] => 1280
            [nb_searches] => 819
            [nb_keywords] => 507
            [avg_time_generation] => 0.832
        )

)

要使用 api 的 getBulkRequest 功能 - 按照这些思路应该可以帮助您入门。

$params=array(
    'module'            =>  'API',
    'method'            =>  'API.getBulkRequest',
    'format'            =>  'json',
    'token_auth'        =>  'anonymous'
);
$urls=array(
    array( 'method' => 'VisitsSummary.get', 'idSite' => 7, 'date' => 'last3', 'period' => 'month' ),
    array( 'method' => 'VisitsSummary.get', 'idSite' => 7, 'date' => 'september', 'period' => 'day' )
);              
$tmp=array();
foreach( $urls as $index => $site ) $tmp[]='urls['.$index.']='.urlencode( http_build_query( $site ) );


/* Set the options for POSTing the data */
$options=array(
    CURLOPT_POSTFIELDS  =>  http_build_query( $params ) . '&' . implode('&',$tmp),
    CURLOPT_POST        =>  true
);


/* Make the request */
$result = curl( $url, $options );

/* If the response status code is 200 (OK) do stuff with data */
if( $result->info->http_code==200 ){
    $data=json_decode( $result->response );
    echo '<pre>',print_r($data,true),'</pre>';
} else {
    /* panic */
    echo "Error: {$result->info->http_code}";
}

以上(对于 getBulkRequests )将输出:

Array
(
    [0] => stdClass Object
        (
            [2017-10] => stdClass Object
                (
                    [nb_uniq_visitors] => 16911
                    [nb_users] => 0
                    [nb_visits] => 21425
                    [nb_actions] => 41266
                    [nb_visits_converted] => 1
                    [bounce_count] => 14400
                    [sum_visit_length] => 2776649
                    [max_actions] => 59
                    [bounce_rate] => 67%
                    [nb_actions_per_visit] => 1.9
                    [avg_time_on_site] => 130
                )

            [2017-11] => stdClass Object
                (
                    [nb_uniq_visitors] => 16108
                    [nb_users] => 0
                    [nb_visits] => 20523
                    [nb_actions] => 39538
                    [nb_visits_converted] => 2
                    [bounce_count] => 13900
                    [sum_visit_length] => 2639042
                    [max_actions] => 171
                    [bounce_rate] => 68%
                    [nb_actions_per_visit] => 1.9
                    [avg_time_on_site] => 129
                )

            [2017-12] => stdClass Object
                (
                    [nb_uniq_visitors] => 12156
                    [nb_users] => 0
                    [nb_visits] => 15199
                    [nb_actions] => 31149
                    [nb_visits_converted] => 4
                    [bounce_count] => 10231
                    [sum_visit_length] => 2069782
                    [max_actions] => 830
                    [bounce_rate] => 67%
                    [nb_actions_per_visit] => 2
                    [avg_time_on_site] => 136
                )

        )

    [1] => stdClass Object
        (
            [nb_uniq_visitors] => 311
            [nb_users] => 0
            [nb_visits] => 347
            [nb_actions] => 592
            [nb_visits_converted] => 2
            [bounce_count] => 229
            [sum_visit_length] => 33879
            [max_actions] => 12
            [bounce_rate] => 66%
            [nb_actions_per_visit] => 1.7
            [avg_time_on_site] => 98
        )

)