demexp-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Demexp-dev] Questions


From: David MENTRE
Subject: Re: [Demexp-dev] Questions
Date: Wed, 17 Oct 2007 18:54:50 +0200
User-agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux)

Hello Lyu,

Lyu Abe <address@hidden> writes:

> can retieve the max number of questions. My PHP code is quite similar:
>
> $message->xmlrpcmsg('max_question_id', $cookie);
> $max_Q_ret = $server->send($message);
> print $max_Q_ret->value();

You have made several errors in this code:
 * In "$message->xmlrpcmsg('max_question_id', $cookie);", you need to
   create a *new* xmlrpcmsg. So you need to use some code like
   "$message = new xmlrpcmsg('max_question_id', ... "

 * For the "$cookie", you need to encode it as XML-RPC value, using
   something like:
    "new xmlrpcval($cookie, "int")"
   Moreover, you forgot to pack it into an array() call.

   Above three points now give:
        $message = new xmlrpcmsg('max_question_id', 
                                 array(new xmlrpcval($cookie, "int")));
        $max_Q_ret = $server->send($message);

 * You neew to convert the returned value in a scalar:
   "print $max_Q_ret->value()->scalarVal();"


This gives the following code:

-----begin example-----
<?php
// One needs http://phpxmlrpc.sourceforge.net/ XML-RPC library to run
// this code.
include 'xmlrpc-2.2/lib/xmlrpc.inc';

// Object to represent the server
$server = new
xmlrpc_client("http://www.linux-france.org/cgi-bin/demexp-xmlrpc-demo";);

// mode debug
$server->setDebug(1);

// Send a message to the server.
$login  =       new xmlrpcval("root", "string");
$passwd =       new xmlrpcval("demexp", "string");

$message = new xmlrpcmsg("login", array($login, $passwd));

$result = $server->send($message);

// Process the response.
if (!$result)
{
     print "<p>Could not connect to HTTP server.</p>";
}
elseif ($result->faultCode())
{
     print "<p>XML-RPC Fault #" . $result->faultCode() . ": " .
     $result->faultString();
}
else
{
        $cookie = $result->value()->scalarVal();
        print "<PRE>\nThe cookie for this session is '". $cookie . "'\n</PRE>";

        $message = new xmlrpcmsg('max_question_id', 
                                 array(new xmlrpcval($cookie, "int")));
        $max_Q_ret = $server->send($message);
        print $max_Q_ret->value()->scalarVal();

}
-----end example-----

Please note this is *very bad* code. No check of error cases, etc. It is
just a quick hack.

This code works and gives the correct number of questions (273).


> I'm stuck!! Help!! ;)

In such a case, please provide the *exact* and *complete* code you are
using (like my example above). Otherwise it is quite complicated for me
to understand what is the issue.


A last note: I find the PHP interface, either the native one (Augustin
code) or the XML-RPC external module (above code), quite cumbersome,
complex to use and error prone. As an example, here is some Python code
doing the same thing:

-----begin Python example-----
#!/usr/bin/python

from xmlrpclib import *
import sys

login = "demo"
password = "demo"
url = "http://www.linux-france.org/cgi-bin/demexp-xmlrpc-demo";

print "Connect to server '%s'" % url
s = ServerProxy(url)

print "login()"
cookie = s.login(login, password)
print " => %s" % cookie


print "max_question_id()"
max_question_id = s.max_question_id(cookie)
print " => %s" % max_question_id

print "goodbye()"
s.goodbye(cookie)
-----end Python example-----


Do you really want to use PHP? :-)

In any case, if you prefer PHP, it might be better to use Augustin's
code. We know it works and it does not need an external library, so it
is going to be easier to deploy. Moreover, I don't find that the PHP
xml-rpc external module improves code's clarity.

Best wishes,
d.
-- 
GPG/PGP key: A3AD7A2A David MENTRE <address@hidden>
 5996 CC46 4612 9CA4 3562  D7AC 6C67 9E96 A3AD 7A2A




reply via email to

[Prev in Thread] Current Thread [Next in Thread]