@chriscroome opened this Issue on March 9th 2016

With Piwik 2.16.0 the only result I get from a URL like this:

is:

<?xml version="1.0" encoding="utf-8" ?>
<result />

Can anyone confirm that this API method works? I'm I doing something stupid?

@chriscroome commented on March 9th 2016

Here is an example of this issue (excuse the Let's Encrypt staging cert), this site:

Is using Piwik running at https://piwik.wsh.webarchitects.org.uk/ and has the site id 44 but this API URL

Returns:

<?xml version="1.0" encoding="utf-8" ?>
<result />

When I'd expect the result to be:

<?xml version="1.0" encoding="utf-8" ?>
<result>
    <row>
        <idsite>44</idsite>
    </row>
</result>

As illustrated here:

What am I doing wrong? Or is there a problem with Piwik?

The System Check reports:

shell_exec
You need to enable this built-in function.

However after enabling that in php.ini I get the same results so I have disabled it again.

@tsteur commented on March 9th 2016 Owner

I had a look at the code and only thing I could think of, that for some reason your URL maybe has a trailing slash saved in the database? I noticed when adding a site url, adding a site or updating a site, we do remove all trailing slashes from all urls. In the API method getSitesIdFromSiteUrl we also remove the trailing slash from the URL. So if for some reason, your URL stored in the database has a trailing slash, it would never find a matching site.

That's just a random guess as the URL should not be saved with a trailing slash.

Another idea would be that the auth token you are using does not have at least view access for that website.

shell_exec should not be needed btw

@tsteur commented on March 9th 2016 Owner

Maybe check in the UI or database (piwik_site.main_url and piwik_site_url table) if all your website URLs have no trailing slash and whether it works maybe with a different token so we can close the issue.

@chriscroome commented on March 9th 2016

No trailing slash in the database:

mysql> SELECT main_url FROM piwik_site WHERE idsite=44;
+-----------------------------------------------------------------------------------------------------+
| main_url                                                                                            |
+-----------------------------------------------------------------------------------------------------+
| http://demo.wsh.webarchitects.org.uk http://demo.wsh.webarchitects.coop http://demo.wsh.webarch.net |
+-----------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

But perhaps this is the issue, there is no matching row in the piwik_site_url table:

mysql> SELECT url FROM piwik_site_url WHERE idsite=44;
Empty set (0.00 sec)

The auth token I was using is the admin one and it doesn't have a problem creating users or adding sites.

@tsteur commented on March 9th 2016 Owner

It doesn't have to have an entry in piwik_site_url. It stores URLs for a site there only if there's more than one URL for a site. The first URL is always stored as main_url so far.

Your main_url entry looks weird. There should be only one URL, the 2 other ones should be actually stored in piwik_site_url. Maybe you entered the URLs whitespace separated instead of separated by a new line? The help text to that field says they should be entered line by line but it's not very user friendly. Maybe, we could at some point also parse whitespace separated URLs.

@chriscroome commented on March 9th 2016

Maybe you entered the URLs whitespace separated instead of separated by a new line?

Yes, sorry, that was my mistake, they were separated with %20 when added via the API, I'll switch to using %0d and let you know how I get on...

@tsteur commented on March 9th 2016 Owner

Maybe try &urls[]=http...&urls[]=http...

@chriscroome commented on March 9th 2016

I tried like this:

https://piwik.wsh.webarchitects.org.uk/?module=API&method=SitesManager.addSite&siteName=demo4.wsh.webarchitects.org.uk&urls=[]http://demo4.wsh.webarchitects.org.uk&urls=[]http://demo4.wsh.webarch.co.uk&urls=[]http://demo4.wsh.webarch.net&timezone=UTC&currency=GBP&format=xml&token_auth=XXX

But that doesn't return a Site ID, and doesn't result in a site being added to Piwik, have I made a mistake in the formatting of the URL?

@chriscroome commented on March 9th 2016

This seems to work OK (URL encoding carriage returns):

https://piwik.wsh.webarchitects.org.uk/?module=API&method=SitesManager.addSite&siteName=demo5.wsh.webarchitects.org.uk&urls=http://demo5.wsh.webarchitects.org.uk%0dhttp://demo5.wsh.webarch.net%0dhttp://demo5.wsh.webarch.co.uk&timezone=UTC&currency=GBP&format=xml&token_auth=XXX

I'll do some more testing and when I'm confident that it is all OK I'll close this issue, thanks for all the help.

@tsteur commented on March 9th 2016 Owner

You had urls=[], try urls[]=

@chriscroome commented on March 9th 2016

Oops I spoke too soon, using %0d results in no whitespace between the URLs:

mysql> SELECT main_url FROM piwik_site WHERE idsite=45;
+-------------------------------------------------------------------------------------------------+
| main_url                                                                                        |
+-------------------------------------------------------------------------------------------------+
| http://demo5.wsh.webarchitects.org.ukhttp://demo5.wsh.webarch.nethttp://demo5.wsh.webarch.co.uk |
+-------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
@chriscroome commented on March 9th 2016

That did the trick! Sorry for my stupid mistake, using this URL:

https://piwik.wsh.webarchitects.org.uk/?module=API&method=SitesManager.addSite&siteName=demo7.wsh.webarchitects.org.uk&urls[]=http://demo7.wsh.webarchitects.org.uk&urls[]=http://demo7.wsh.webarch.net&urls[]=http://demo7.wsh.webarch.co.uk&timezone=UTC&currency=GBP&format=xml&token_auth=XXX

The sites get added correctly:

mysql> SELECT main_url FROM piwik_site WHERE idsite=47;
+---------------------------------------+
| main_url                              |
+---------------------------------------+
| http://demo7.wsh.webarchitects.org.uk |
+---------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT url FROM piwik_site_url WHERE idsite=47;
+--------------------------------+
| url                            |
+--------------------------------+
| http://demo7.wsh.webarch.co.uk |
| http://demo7.wsh.webarch.net   |
+--------------------------------+
2 rows in set (0.00 sec)

Thanks again for your patience :-)

PS Perhaps adding an example for SitesManager.addSite here: https://developer.piwik.org/api-reference/reporting-api#SitesManager would prevent people like me having problems like this in the future...

@tsteur commented on March 9th 2016 Owner

I just added an example. I'm glad we could fix it. Thx for helping to make our docs better :+1:

This Issue was closed on March 9th 2016
Powered by GitHub Issue Mirror