API doesn't want to work


#1

Hi Guys

Ok, well i’m trying to use the API to add users to my piwik.

I have a problem: It doesn’t work!

I’m following the instructions here:
http://piwik.org/docs/analytics-api/calling-techniques/

I’m using the Call the Piwik API in PHP code.

Now, here is the problem:
“If you are developing a plugin, you have to use this technique.”

I am not developing a plugin. I am at a loss here because I am not developing a plugin but there is no other code sample shown for normal non plugin use other than using http. That is not very helpful.

My directory structure is like this:
/mysite
/mysite/piwik

So i have modified the code (realpath(’/piwik’) returned a empty string):


      define('PIWIK_INCLUDE_PATH', '/piwik');
      define('PIWIK_USER_PATH', '/piwik');
      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();
      // This inits the API Request with the specified parameters
      $request = new Piwik_API_Request('
      			method=UsersManager.addUser
      			&userLogin=$_POST[user]
      			&password=$_POST[password]
               &email=$_POST[email]
               &alias=test
               &token_auth=1e84fb673b0a8ce77b6fdd16af0493d3
      ');
      // Calls the API and fetch XML data back
      $result = $request->process();

All I am seeing is errors that “Class ‘Piwik_FrontController’ not found”


(Thomas Seifert) #2

You need a full server path, not just /piwik
usually something like /home/username/folder … (with replacements obviously ;)).


#3

Thats made no difference at my end.

This is my current code:


      $Path = realpath('./piwik');
      logit($Path);
      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();
      // This inits the API Request with the specified parameters
      $request = new Piwik_API_Request('
      			method=UsersManager.addUser
      			&userLogin=$_POST[user]
      			&password=$_POST[password]
               &email=$_POST[email]
               &alias=test
               &token_auth=1e84fb673b0a8ce77b6fdd16af0493d3
      ');
      // Calls the API and fetch XML data back
      $result = $request->process();


#4

Is there any chance of some proper help here from anyone?

I am getting desperate. My client needs this working ASAP and i can’t get this stupid API to create a user despite following the source sample to the T.

I am becoming more and more tempted just to hack it instead and say balls to this API which clearly doesn’t work.

It’s things like this which frustrate so many programmers - lack of support, lack or caring and lack of decent documentation. I’ve spent over 4 hours on this now.


(Thomas Seifert) #5

Lack of info and swearing from the ones looking for help is also a problem to the ones trying to give you support. Mind that I’m just a user and we are all doing this in our spare time!

So, do you get a real path back now? What does it look like. Are you still getting the same error message as posted above?
I’ve never tried the php api as I’m using the url calls to create users and get data out of it.


#6

MY real path is now looking like this:
D:\UniServer\www\dashboard\piwik

I randomly get a 200 status code and i randomly get 500. It is completely random but nothing appears in the database. No new user at all.

Currently for some odd reason i am getting a 200 error code and this as the html: Not ready…

This is driving me nuts - what am i supposed to do all day? sit here doing nothing? - I can’t, this needs to be working asap.

Unfortunately i can’t use cURL or any form of http request on a server that could have 30k hits per minute. Thats just not practical.


#7

This is my latest reply (again http code is 200 and i’ve changed nothing - just submitted exactly the same form data):

<?xml-stylesheet href="chrome://global/locale/intl.css" type="text/css" ?> XML Parsing Error: no element found Location: chrome://httpfox/content/HttpFoxWindow.xul Line Number 1, Column 1: ^

Does this mean anything to you? Because it means nothing to me other than piwik’s API doesn’t work and has some sort of AI operating forcing it to provide different results randomly.


(Thomas Seifert) #8

Regarding your latest post:
look at the raw output and not what the browser is telling you. The error message there is coming from your browser.

Regarding your code:


$request = new Piwik_API_Request('
      			method=UsersManager.addUser
      			&userLogin=$_POST[user]
      			&password=$_POST[password]
               &email=$_POST[email]
               &alias=test
               &token_auth=1e84fb673b0a8ce77b6fdd16af0493d3
      ');

That can’t work. You are using ’ which means that nothing inside that string is replaced, therefore your $_POST… variables are not taken as variables but as strings. Basic PHP.

Regarding your sitting there: do some debugging, dive into the code as we all do when errors occur. This is an open source project, it will benefit from everyone who knows the code and reports issues where or whatever.


#9

I can’t believe i forgot about the ’ instead of ". You’d think that would be mentioned in the demo source code wouldn’t you.

I’ve changed that, and its still not working. I still keep getting the ‘RAW’ reply of “Not ready…”. I am using the HttpFox plugin to look at the headers that are being sent and received NOT the html web page - I’ve had to debug a lot of complex stuff in the past.

With regards to debugging, sure if i had more time (say a few weeks) I could certainly do that but i do not have the time to learn piwik inside out, through over 100 php files just to find out why their API doesn’t work - which they provide to make life easy. It’s not realistically practical is it.


#10

Ok, i’ve got it working… finally…

Thanks for your help. You have no idea how stressed i felt…


(Thomas Seifert) #11

Maybe you could elaborate what the solution or issue was in the end?


#12

MY stupidity.

Instead of echo’ing $result (which was rather useful) i was echoing my own custom result. When i finally echo’d the $result (which i had initially done but realpath() made me change things) i finally got some sort of useful error message.

Incidentally the “Not ready…” message turned out to be HttpFox - when i clicked off the request and back onto it there was output so i think this was simply HttpFox displaying the ajax status.