webERP Forum

Full Version: API (xmlrpc) problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I'm trying to connect/login with the API following the conventions in the example on this page:
http://www.weberp.org/wiki/APIDocumentation

Something is going wrong when I call xmlrpcval to try to set the values of the $Parameters array, I'm not sure why because I haven't strayed much from the example above.

Code:
<?php
$dbname = "smt_weberp";
$username = "zzzzzzzzz";
$password = "zzzzzzzzz";

echo ("<p>Testing webERP API login...</p>");

include ("xmlrpc/lib/xmlrpc.inc");

$ServerURL = "http://webtest/webERP/api/api_xml-rpc.php";
$DebugLevel = 2;

$Parameters = array();
echo ("<p>We've made it this far!</p>");

$Parameters['DatabaseName'] = xmlrpcval($dbname);
$Parameters['Username'] = xmlrpcval($username);
$Parameters['Password'] = xmlrpcval($password);
echo ("This doesn't show up!");
var_dump($Parameters);
?>

The location of the xmlrpc include is correct (I've put echos in there to verify and they print out fine) and the URL for api_xml-rpc.php is good (not that I'm even getting that far yet.) I have all of the contents of the "xmlrpc" folder in the proper place within the folder of the php file I've posted above (sitting on the same web server as the webERP installation but in a different folder under the root.)

My "We've made it this far!" prints, but "This doesn't show up!", well, doesn't show up, and the var_dump doesn't show either. I'm not sure if my 'DatabaseName' parameter key is correct but at this point all I'm trying to do is format these parameters with xmlrpcval and this seems to be stopping PHP parsing.

I must be missing a very simple step but I don't see anything going on in the example that I'm failing to do here. Can anybody set me straight?

I'm running php version 5.3.5 on the web server, if that matters.

Thank you for any help!
Check out your web-server error log - looks like there may be some parse error .. I am not sure that echoing out variables works with the xml-rpc script - I know that debugging the API is miserable. I'd say the web-server error log will have the answer though.
Apparently echoing out variables from an API script defeats the code in:

Code:
include ("xmlrpc/lib/xmlrpc.inc");

So you must remove the echos from your script - your script doesn't actually call any API methods - need to look again at the example at:

http://www.weberp.org/wiki/APIDocumentation
Thanks for the info, Phil.

I copied the example you linked verbatim, and I added DVD-TOPGUN to my inventory and gave it a quantity of 5 at my only location. The only change I made to the code from the example is the $ServerURL variable, which I've pointed to the appropriate location on my server, and the username/password combo matches already so no need to mess with that.

It's not giving me anything back, if I view source it's completely blank. Regarding the echos before calling xmlrpcval, the example you link is echoing as well (just the opening html tags and such.) Even if I comment out the html echos, I'm getting no "location x has 5 on hand" or anything like that.

I'm thinking something is fundamentally weird with either my PHP setup or the example, and I'm not enough of a PHP guru to really pin it down.

I know this is not your code, but looking into xmlrpc.inc, I see xmlrpcval is a class unto itself, with a constructor function xmlrpcval. Forgive my PHP newbishness (at least when it comes to the class/object stuff) but my Java-trained brain is inclined to think just calling xmlrpcval like a regular function in the example will not work. Again, this is me stuck in Java mode, so maybe this is kosher in PHP?
There is some information on the mailing lists archives which may be of some use - I recall I had similar difficulties getting going - can't sadly recall the answer sorry - have a look through some of these old postings though - maybe of some help. I know setting the debug level produced some output which enabled me to figure out what was going on.

http://sourceforge.net/mailarchive/messa...d=22256123
Well, I've gotten a little farther under the hood with this and I've made some progress, but I'm hitting another weird snag that I can't explain. The problem appears to be in xmlrpc-land.

It looks like there is a flaw in the example at:
http://www.weberp.org/wiki/APIDocumentation

This kind of thing does not work:
$Parameters['StockID'] = xmlrpcval('DVD-TOPGUN');

It should be:
$Parameters['StockID'] = new xmlrpcval('DVD-TOPGUN');
(as it is in the mailing list post you linked, thanks Phil!)

xmlrpcval is a class, not a function, so an object needs to instantiated with "new" to call the constructor function xmlrpcval.

Soooo, having made that adjustment, I build the $Parameters array of xmlrpcval objects, which works fine.

Doing a var_dump on it, I get:
Code:
array(3) {
  ["StockID"]=>
  object(xmlrpcval)#1 (3) {
    ["me"]=>
    array(1) {
      ["string"]=>
      string(10) "DVD-TOPGUN"
    }
    ["mytype"]=>
    int(1)
    ["_php_class"]=>
    NULL
  }
  ["Password"]=>
  object(xmlrpcval)#2 (3) {
    ["me"]=>
    array(1) {
      ["string"]=>
      string(6) "weberp"
    }
    ["mytype"]=>
    int(1)
    ["_php_class"]=>
    NULL
  }
  ["Username"]=>
  object(xmlrpcval)#3 (3) {
    ["me"]=>
    array(1) {
      ["string"]=>
      string(5) "admin"
    }
    ["mytype"]=>
    int(1)
    ["_php_class"]=>
    NULL
  }
}

This looks perfect.

But here's where things get weird:
$msg = new xmlrpcmsg("weberp.xmlrpc_GetStockBalance", $Parameters);

Then a var_dump of $msg looks like this:
Code:
object(xmlrpcmsg)#4 (5) {
  ["payload"]=>
  NULL
  ["methodname"]=>
  string(29) "weberp.xmlrpc_GetStockBalance"
  ["params"]=>
  array(0) {
  }
  ["debug"]=>
  int(0)
  ["content_type"]=>
  string(8) "text/xml"
}

What happened to my parameters? Sad

So digging into xmlrpc, here is the constructor for xmlrpcmsg:
Code:
function xmlrpcmsg($meth, $pars=0)
{
    $this->methodname=$meth;
    if(is_array($pars) && count($pars)>0)
    {
        for($i=0; $i<count($pars); $i++)
        {
            $this->addParam($pars[$i]);
        }
    }
}

NOTE: I have tried getting rid of the "=0" after $pars, no effect.

So, we follow the code to addParam, which looks like this:
Code:
function addParam($par)
{
    // add check: do not add to self params which are not xmlrpcvals
    if(is_object($par) && is_a($par, 'xmlrpcval'))
    {
        $this->params[]=$par;
        return true;
    }
    else
    {
        return false;
    }
}

is_object($par) and is_a($par, 'xmlrpcval') are both returning false. I'm a little lost as to why that would be...

It's looking like this isn't really a webERP issue at this point, but I'd appreciate it if anyone who has run into anything similar could point me in the right direction here.
Sorry this is too tricky for me ... I will have a play and see if the example I published still works for me!!

If you run it without the var_dumps do you get any output from the call??

The API is definitely alive and well though as it is working with my POS system.

I have updated my example in the docs to use the new keyword - thanks for the feedback.

Did you ever play with the example scripts that Tim published that are downloadable from the wiki page?
edit: The link didn't work - just fixed it http://www.weberp.org/wiki/uploads/Scree...xmlrpc.zip

Tim's code looks like:
Code:
<?php
    include '../webERP/xmlrpc/lib/xmlrpc.inc';

    include 'config.inc';

    echo '<a href="index.php">Home</a></BR>';

    if (isset($_GET['stockid'])) {
        $_POST['stockid']=$_GET['stockid'];
        $_POST['submit']='set';
    }
    if (!isset($_POST['submit'])) {
        echo '<FORM METHOD="post" action=' . $_SERVER['PHP_SELF'] . '>Enter stock id';
        echo '<INPUT type="text" name="stockid"></BR>';
        echo 'Enter Location ID<INPUT type="text" name="locationid"></BR><input type="Submit" name="submit" value="Get Balance"';
        echo '</FORM>';
    } else {
        $stockid = new xmlrpcval($_POST['stockid']);
        $user = new xmlrpcval($weberpuser);
        $password = new xmlrpcval($weberppassword);
      
        $msg = new xmlrpcmsg("weberp.xmlrpc_GetStockBalance", array($stockid, $user, $password));
        
        
        $client = new xmlrpc_client($ServerURL);
        $client->setDebug($DebugLevel);

        $response = $client->send($msg);
        $balances = php_xmlrpc_decode($response->value());
        echo "<table border=1>";
        if (sizeof($balances)<1) {
            echo 'oops, an error number '.$balances[0].' has occurred';
        }
        for ($i=0;$i<sizeOf($balances[1]);$i++) {
            echo "<tr><td>".$balances[1][$i]['loccode']."</td><td>".$balances[1][$i]['quantity']."</td></tr>";
        }
        echo "</table>";
    }
?>
You need the little config.inc file too for the username pwd host etc.
I tried Tim's example - there's a little glitch in the code of his that you posted, the submit button needs a closing ">".

I'm getting the same kind of results as before, it seems. With no echos at all, letting Tim's code do its thing (with debuglevel set to 2), this is the output I get:

Code:
---SENDING---
POST /webERP/api/api_xml-rpc.php HTTP/1.0
User-Agent: XML-RPC for PHP 3.0.0.beta
Host: webtest
Accept-Encoding: gzip, deflate
Accept-Charset: UTF-8,ISO-8859-1,US-ASCII
Content-Type: text/xml
Content-Length: 293

<?xml version="1.0"?>
<methodCall>
<methodName>weberp.xmlrpc_GetStockBalance</methodName>
<params>
<param>
<value><string>DVD-TOPGUN</string></value>
</param>
<param>
<value><string>admin</string></value>
</param>
<param>
<value><string>weberp</string></value>
</param>
</params>
</methodCall>
---END---

---GOT---
HTTP/1.1 200 OK
Date: Mon, 25 Feb 2013 15:35:13 GMT
Server: Apache/2.2.17 (Ubuntu)
X-Powered-By: PHP/5.3.5-1ubuntu7.11
Set-Cookie: webERPapi=oc92qjdlkmse6667vq7bhiq703; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 119
Connection: close
Content-Type: text/xml

‹³±¯ÈÍQ(K-*ÎÌϳU2Ô3P²·ã²ÉM-ÉÈO    J-.ÈÏ+N
$%æÃ@º,1§4ÕÎ&±¨(±ÈMI,IDˆfæ•ØÚèƒ(}ˆ—>T>D’ÌL}¸-úhï…ë§
---END---

HEADER: date: Mon, 25 Feb 2013 15:35:13 GMT
HEADER: server: Apache/2.2.17 (Ubuntu)
HEADER: x-powered-by: PHP/5.3.5-1ubuntu7.11
HEADER: set-cookie: webERPapi=oc92qjdlkmse6667vq7bhiq703; path=/
HEADER: expires: Thu, 19 Nov 1981 08:52:00 GMT
HEADER: cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
HEADER: pragma: no-cache
HEADER: vary: Accept-Encoding
HEADER: content-encoding: gzip
HEADER: content-length: 119
HEADER: connection: close
HEADER: content-type: text/xml
COOKIE: webERPapi=oc92qjdlkmse6667vq7bhiq703

---INFLATED RESPONSE---[167 chars]---
<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value><array>
<data>
<value><int>1</int></value>
</data>
</array></value>
</param>
</params>
</methodResponse>
---END---

---PARSED---
xmlrpcval::__set_state(array(
   'me' =>
  array (
    'array' =>
    array (
      0 =>
      xmlrpcval::__set_state(array(
         'me' =>
        array (
          'int' => 1,
        ),
         'mytype' => 1,
         '_php_class' => NULL,
      )),
    ),
  ),
   'mytype' => 2,
   '_php_class' => NULL,
))
---END---

If I do a view source, the <table border=1></table> is at the bottom but there is no "oops, an error number." The $balances array has a size of 1, should the if statement therefore be if (sizeof($balances)<=1) instead of <1?
If you run your client against the demo at http://www.weberp.org/weberp/api/api_xml-rpc.php does it work?

Can you run webERP ok on http://webtest/webERP/ ? No apache protection or something ?

I feel as though I have had this error myself but cannot recall what it was.... sorry.
Ahh, interesting, when I point to the instance of the API on your domain and search for inventory items in your demo instance, I DO get results back as expected.

I'm able to run webERP at http://webtest/webERP/ just fine, it doesn't look like a folder permissions thing, but I'll try to investigate that possibility further.

Suspicions/fears confirmed, I guess this is something to do with xmlrpc on my end. This is all kind of new to me, I'm used to using JSON for just about everything like this.
Pages: 1 2