I have used the following code to remove any duplicate website entries in a piwik site statistics package – sometimes occurs if account addition apis are used elsewhere. Replace yourtoken with your piwik api token, and http://yoursite/piwik with your piwik sites main url path – do not include a final / at the end. Hope this helps someone.


set_time_limit(0);
error_reporting(0);

$token = "yourtoken";
$piwiksite = "http://yoursite/piwik";


$get_sites = $piwiksite."/index.php?module=API&method=SitesManager.getAllSites&format=xml&token_auth=".$token;

$xml=file_get_contents($get_sites);

$rows = new SimpleXMLElement($xml);

$names=array();
$dups=array();
foreach ($rows->xpath('//row') as $row) {
$id=$row->idsite;
$name=$row->name;
$name=str_ireplace('http:','',$name);
$name=str_ireplace('www.','',$name);
$name=strtolower($name);
if(in_array($name,$names)===false) {
$names[]=$name;
} else {
$dups[]=$id;
}
}

if(count($dups)>0) {
foreach($dups as $id) {
$delete_site = $piwiksite."/index.php?module=API&method=SitesManager.deleteSite&idSite=".$id."&token_auth=".$token;
$result=file_get_contents($delete_site);
echo($result);
echo("\n");
}
}