Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API method SitesManager.getSitesIdFromSiteUrl doesn't return a Site ID #9901

Closed
chriscroome opened this issue Mar 9, 2016 · 13 comments
Closed
Labels
answered For when a question was asked and we referred to forum or answered it.
Milestone

Comments

@chriscroome
Copy link

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
Copy link
Author

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.

@chriscroome chriscroome changed the title API method SitesManager.getSitesIdFromSiteUrl don't return a Site ID API method SitesManager.getSitesIdFromSiteUrl doesn't return a Site ID Mar 9, 2016
@tsteur
Copy link
Member

tsteur commented Mar 9, 2016

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
Copy link
Member

tsteur commented Mar 9, 2016

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
Copy link
Author

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
Copy link
Member

tsteur commented Mar 9, 2016

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
Copy link
Author

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
Copy link
Member

tsteur commented Mar 9, 2016

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

@chriscroome
Copy link
Author

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
Copy link
Author

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
Copy link
Member

tsteur commented Mar 9, 2016

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

@chriscroome
Copy link
Author

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
Copy link
Author

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 tsteur closed this as completed in 687e459 Mar 9, 2016
@tsteur
Copy link
Member

tsteur commented Mar 9, 2016

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

@tsteur tsteur added the answered For when a question was asked and we referred to forum or answered it. label Mar 9, 2016
@tsteur tsteur added this to the 2.16.1 milestone Mar 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered For when a question was asked and we referred to forum or answered it.
Projects
None yet
Development

No branches or pull requests

2 participants