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:


http://example.com/piwik/?module=API&method=SitesManager.getAllSitesId&url=example.com&token_auth=xxxxxxxxxxx

This quite happily returns the single domain’s id:


<result>
  <row>1</row>
</result>

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


http://example.com/piwik/?module=API&method=SitesManager.getSiteFromId&idSite=1&token_auth=xxxxxxxxxxx

which returns:


<result>
  <row>
    <idsite>1</idsite>
    <name>example.com</name>
    <main_url>http://example.com</main_url>
    <ts_created>2011-04-08 04:23:25</ts_created>
    <timezone>Australia/Brisbane</timezone>
    <currency>USD</currency>
    <excluded_ips/>
    <excluded_parameters/>
    <group/>
    <feedburnerName/>
  </row>
</result>

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


http://example.com/piwik/?module=API&method=SitesManager.getSitesIdFromSiteUrl&url=http://example.com/&token_auth=xxxxxxxxxxx

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


<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";
  Piwik_FrontController::getInstance()->init();
  $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.