Hello guys, i need to fetch data for brand and model from device but it always returning me “0 results” while i use another script with partially the same as this one and work.
<?php
<?php
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";
*/
$urlz='https://xxxxxx.xxxx/xxxxxx/';
/* original url as array of parameters to POST */
$params=array(
'module' => 'API',
'method' => 'Actions.get',
'idSite' => 1,
'date' => 'last3',
'format' => 'php',
'filter_sort_order' => 'desc',
'token_auth' => 'xxxxxx'
);
/* Set the options for POSTing the data */
$options=array(
CURLOPT_POSTFIELDS => http_build_query( $params ),
CURLOPT_POST => true
);
/* Make the request */
$result = curl( $urlz, $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}";
}
$params=array(
'module' => 'API',
'method' => 'API.getBulkRequest',
'format' => 'json',
'token_auth' => 'xxxxxx'
);
$urls=array(
array( 'method' => 'DevicesDetection.getBrand', 'idSite' => 1, 'date' => 'today', 'period' => 'month' ),
array( 'method' => 'DevicesDetection.getModel', 'idSite' => 1, 'date' => 'today', 'period' => 'month' )
);
$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( $urlz, $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}";
}
?>
While this one return me the right result:
// this token is used to authenticate your API request.
// You can get the token on the API page inside your Matomo interface
$token_auth = 'xxxx';
// we call the REST API and request the 100 first keywords for the last month for the idsite=62
$url = "https://xxxx.xxxx/xxx/";
$url .= "?module=API&method=UserCountry.getCity";
$url .= "&idSite=2&period=month&date=today";
$url .= "&format=JSON&filter_limit=10";
$url .= "&token_auth=$token_auth&language=fr&language=fr";
$fetched = file_get_contents($url);
$content = json_decode($fetched,true);
foreach ($content as $row) {
$countryName = htmlspecialchars($row["label"], ENT_QUOTES, 'UTF-8');
$logo = htmlspecialchars($row["logo"], ENT_QUOTES, 'UTF-8');
$hits = $row['nb_visits'];
print("<tr><td><img src='https://xxxxxx.xxxx/xxxxx/$logo' style='height: 12px !important; width: 18px; !important'/> <b>$countryName</b></td> <td>$hits visite(s)</td><br>\n</tr>");
}
if (!$content) {
print(" <p class='text-danger'><b>no data</p>");
}
Somebody already have this problem ? thanks for help