API call to SitesManager.getSitesIdFromSiteUrl doesn' seem to respond

I’m new to piwik so I suspect that I’ve just missed something simple, but for the life of me I can’t figure out how to get the id of a site from the url.

To reproduce the error, I’ve followed the steps below:

I have set up Piwik on a sandbox domain, example.com/piwiki. The first thing I do is try to get a list of all domains and their ids (just one in this case) so I submit the following url:


This quite happily returns the single domain’s id:


So now I use SitesManager.getSiteFromId to get the site information:


which returns:

    <ts_created>2011-04-08 04:23:25</ts_created>

Great, now I know that the url of the domain is “http://example.com”, so know I pass this on to SitesManager.getSitesIdFromSiteUrl:


But this just returns a 404 error! If I set “url” to simply “example.com” I get an empty xml result:


What have I missed? I have mod_security enabled, could this have something to do with it?

you must urlencode() the URL before passing it Piwik which might be the issue?

That could have been it, I’ve since realised that a far more elegant way to programmatically call the API is this:

  //$path is the path to piwik
  //$query is the query string to pass to the API
  define('PIWIK_INCLUDE_PATH', $path);
  define('PIWIK_USER_PATH', $path);
  define('PIWIK_ENABLE_DISPATCH', false);
  define('PIWIK_ENABLE_ERROR_HANDLER', false);
  define('PIWIK_ENABLE_SESSION_START', false);
  require_once PIWIK_INCLUDE_PATH . "/index.php";
  require_once PIWIK_INCLUDE_PATH . "/core/API/Request.php";
  $request = new Piwik_API_Request($query);
  return $request->process();

So I’m going with that instead. Hope this helps someone.

It’s probably a mod_security thing. For example, Hostgator’s mod_security rules block any requests containing “http://” in the URL’s query parameters.

Well I am using Hostgator so that might be it. Any idea about my latest dilema (301 Moved Permanently)?

Ask HG to have your domain whitelisted against the mod_security rules.