Results 1 to 3 of 3

Thread: Xml-rpc

  1. #1
    galaxyAbstractor's Avatar
    galaxyAbstractor is offline Community Advocate
    Join Date
    Oct 2007
    Location
    Land of Null and Insanity
    Posts
    5,503

    Xml-rpc

    I am experimenting with XML-RPC, but I'm kind of stuck.

    The serverside is PHP, while the client is in JAVA

    Server:
    PHP Code:
    <?php
    /*
    * First, we define some PHP functions to expose via
    * XML-RPC. Any functions that will be called by a
    * XML-RPC client need to take three parameters:
    * The first parameter passed is the name of the
    * XML-RPC method called, the second is an array
    * Containing the parameters sent by the client, and
    * The third is any data sent in the app_data
    * parameter of the xmlrpc_server_call_method()
    * function (see below).
    */
    function uptime_func($method_name$params$app_data)
    {
    return 
    "blahblah";
    }
    function 
    greeting_func($method_name$params$app_data)
    {
    $name $params[0];
    return 
    "Hello, $name. How are you today?";
    }
    /*
    * This creates a server and sets a handle for the
    * server in the variable $xmlrpc_server
    */
    $xmlrpc_server xmlrpc_server_create();
    /*
    * xmlrpc_server_register_method() registers a PHP
    * function as an XML-RPC method. It takes three
    * parameters:
    * The first is the handle of a server created with
    * xmlrpc_server_create(), the second is the name to
    * register the server under (this is what needs to
    * be in the <methodName> of a request for this
    * method), and the third is the name of the PHP
    * function to register.
    */
    xmlrpc_server_register_method($xmlrpc_server"greeting""greeting_func");
    xmlrpc_server_register_method($xmlrpc_server"uptime""uptime_func");
    /*
    * When an XML-RPC request is sent to this script, it
    * can be found in the raw post data.
    */
    $request_xml $HTTP_RAW_POST_DATA;
    /*
    * The xmlrpc_server_call_method() sends a request to
    * the server and returns the response XML. In this case,
    * it sends the raw post data we got before. It requires
    * 3 arguments:
    * The first is the handle of a server created with
    * xmlrpc_server_create(), the second is a string containing
    * an XML-RPC request, and the third is for application data.
    * Whatever is passed into the third parameter of this function
    * is passed as the third paramater of the PHP function that the
    * request is asking for.
    */
    $response xmlrpc_server_call_method($xmlrpc_server$request_xml'');
    // Now we print the response for the client to read.
    print $response;
    /*
    * This method frees the resources for the server specified
    * It takes one argument, a handle of a server created with
    * xmlrpc_server_create().
    */
    xmlrpc_server_destroy($xmlrpc_server);
    ?>
    Client:
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package xmlrpcclient;
    
    import java.net.URL;
    import org.apache.xmlrpc.client.XmlRpcClient;
    import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
    
    class Main {
    
    public static void main( String[] args ) throws Exception
    {
    
    
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setServerURL(new URL("http://localhost/xml/index.php"));
        XmlRpcClient client = new XmlRpcClient();
        client.setConfig(config);
        Object[] params = new Object[]{new Integer(33), new Integer(9)};
        Integer result = (Integer) client.execute("uptime", params);
        System.out.println(result);
       
    }
    
    }
    I am using Apache XML-RPC as java package, http://ws.apache.org/xmlrpc/

    I get this error:
    run:
    Exception in thread "main" org.apache.xmlrpc.client.XmlRpcClientException: Failed to parse server's response: Expected methodResponse element, got br
    at org.apache.xmlrpc.client.XmlRpcStreamTransport.rea dResponse(XmlRpcStreamTransport.java:180)
    at org.apache.xmlrpc.client.XmlRpcStreamTransport.sen dRequest(XmlRpcStreamTransport.java:14
    at org.apache.xmlrpc.client.XmlRpcHttpTransport.sendR equest(XmlRpcHttpTransport.java:115)
    at org.apache.xmlrpc.client.XmlRpcSunHttpTransport.se ndRequest(XmlRpcSunHttpTransport.java:69)
    at org.apache.xmlrpc.client.XmlRpcClientWorker.execut e(XmlRpcClientWorker.java:56)
    at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlR pcClient.java:167)
    at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlR pcClient.java:137)
    at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlR pcClient.java:126)
    at xmlrpcclient.Main.main(Main.java:23)
    Caused by: org.xml.sax.SAXParseException: Expected methodResponse element, got br
    at org.apache.xmlrpc.parser.XmlRpcResponseParser.star tElement(XmlRpcResponseParser.java:101)
    at com.sun.org.apache.xerces.internal.parsers.Abstrac tSAXParser.startElement(AbstractSAXParser.java:501 )
    at com.sun.org.apache.xerces.internal.parsers.Abstrac tXMLDocumentParser.emptyElement(AbstractXMLDocumen tParser.java:179)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocum entScannerImpl.scanStartElement(XMLNSDocumentScann erImpl.java:377)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocum entScannerImpl$NSContentDriver.scanRootElementHook (XMLNSDocumentScannerImpl.java:626)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumen tFragmentScannerImpl$FragmentContentDriver.next(XM LDocumentFragmentScannerImpl.java:3095)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumen tScannerImpl$PrologDriver.next(XMLDocumentScannerI mpl.java:921)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumen tScannerImpl.next(XMLDocumentScannerImpl.java:64
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocum entScannerImpl.next(XMLNSDocumentScannerImpl.java: 140)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumen tFragmentScannerImpl.scanDocument(XMLDocumentFragm entScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Co nfiguration.parse(XML11Configuration.java:807)
    at com.sun.org.apache.xerces.internal.parsers.XML11Co nfiguration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLPars er.parse(XMLParser.java:107)
    at com.sun.org.apache.xerces.internal.parsers.Abstrac tSAXParser.parse(AbstractSAXParser.java:1205)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserI mpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
    at org.apache.xmlrpc.client.XmlRpcStreamTransport.rea dResponse(XmlRpcStreamTransport.java:17
    ... 8 more
    Caused by:
    org.xml.sax.SAXParseException: Expected methodResponse element, got br
    at org.apache.xmlrpc.parser.XmlRpcResponseParser.star tElement(XmlRpcResponseParser.java:101)
    at com.sun.org.apache.xerces.internal.parsers.Abstrac tSAXParser.startElement(AbstractSAXParser.java:501 )
    at com.sun.org.apache.xerces.internal.parsers.Abstrac tXMLDocumentParser.emptyElement(AbstractXMLDocumen tParser.java:179)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocum entScannerImpl.scanStartElement(XMLNSDocumentScann erImpl.java:377)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocum entScannerImpl$NSContentDriver.scanRootElementHook (XMLNSDocumentScannerImpl.java:626)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumen tFragmentScannerImpl$FragmentContentDriver.next(XM LDocumentFragmentScannerImpl.java:3095)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumen tScannerImpl$PrologDriver.next(XMLDocumentScannerI mpl.java:921)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumen tScannerImpl.next(XMLDocumentScannerImpl.java:64
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocum entScannerImpl.next(XMLNSDocumentScannerImpl.java: 140)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumen tFragmentScannerImpl.scanDocument(XMLDocumentFragm entScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Co nfiguration.parse(XML11Configuration.java:807)
    at com.sun.org.apache.xerces.internal.parsers.XML11Co nfiguration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLPars er.parse(XMLParser.java:107)
    at com.sun.org.apache.xerces.internal.parsers.Abstrac tSAXParser.parse(AbstractSAXParser.java:1205)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserI mpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
    at org.apache.xmlrpc.client.XmlRpcStreamTransport.rea dResponse(XmlRpcStreamTransport.java:17
    at org.apache.xmlrpc.client.XmlRpcStreamTransport.sen dRequest(XmlRpcStreamTransport.java:14
    at org.apache.xmlrpc.client.XmlRpcHttpTransport.sendR equest(XmlRpcHttpTransport.java:115)
    at org.apache.xmlrpc.client.XmlRpcSunHttpTransport.se ndRequest(XmlRpcSunHttpTransport.java:69)
    at org.apache.xmlrpc.client.XmlRpcClientWorker.execut e(XmlRpcClientWorker.java:56)
    at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlR pcClient.java:167)
    at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlR pcClient.java:137)
    at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlR pcClient.java:126)
    at xmlrpcclient.Main.main(Main.java:23)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    Any ideas?
    Last edited by galaxyAbstractor; 02-19-2009 at 01:47 PM.

  2. #2
    misson is offline x10 Spammer
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,573

    Re: Xml-rpc

    The code you posted works for me. The message "Expected methodResponse element, got br" leads me to believe the client is getting HTML (specifically, a <br /> element) rather than an XML-RPC message. Access http://localhost/xml/index.php in a browser & see what you get. My guesses are the page is throwing an HTML formatted error message or the URL is wrong.

    (quick note: when playing around with XML-RPC, it's incredibly easy to create a client in Python:
    Code:
    #!/usr/local/bin/env python
    
    import xmlrpclib
    
    serv=xmlrpclib.ServerProxy("http://localhost/xml/index.php")
    
    print serv.uptime()
    print serv.greeting('foo');
    By the way, thanks for posting an informative help request. You even had a minimal test case!
    Last edited by misson; 02-20-2009 at 04:31 AM.

  3. #3
    galaxyAbstractor's Avatar
    galaxyAbstractor is offline Community Advocate
    Join Date
    Oct 2007
    Location
    Land of Null and Insanity
    Posts
    5,503

    Re: Xml-rpc

    Nah I got it. XML-RPC wasn't installed in LAMPP

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
dedicated servers