Um die getBulkRequest
zu verwenden zu erleichtern (Details, von denen ich nicht auf ihrer Website finden konnte) oder sogar die anderen API-Aufrufe finden Sie, dass die Verwendung von curl einfacher ist, da Sie die Anfrage schnell anpassen können, indem Sie einige Optionen an die unten stehende curl-Funktion ändern und eine andere $params
Nutzlast konstruieren.
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}";
}
Der obige Ausgang folgend:
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
)
)
Um das getBulkRequest
Merkmal des api verwenden - etwas in diese Richtung sollten Sie begonnen erhalten.
$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}";
}
Die oben (für getBulkRequests
) folgende Ausgabe:
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
)
)
Und warum sind zwei separate Anrufe nicht möglich? –
Warum funktioniert ihr 'API.getBulkRequest' nicht in PHP? – RamRaider
Sie benötigen das Format entweder JSON oder XML. – Darren