Question

In my website I need to configure the REST, XML-RPC, SOAP servers.

REST: I have used the FriendsOfSymfony REST bundle
SOAP: PHP SOAP used (doc from symfony website
XML-RPC: I have planed to use the Zend XmlRpc

How to configure the Zend XmlRpc server in Symfony?

Any useful links with step by step configuration?

Or any other XML-RPC which can be used with symfony

Thanks in advance, SVN

Was it helpful?

Solution

I have used zend xmlrpc with symfony

composer.json

"zendframework/zend-xmlrpc": "2.1.*"

config.yml

services:
   MyTestService:
        class: Acme\DemoBundle\Controller\MyTestService
        arguments: ["@doctrine.orm.entity_manager"] 

routing.yml

_xmlrpc:
    pattern:  /xmlrpc
    defaults: { _controller: AcmeDemoBundle:Xmlrpc:index }
_xmlrpc_test:
    pattern:  /xmlrpc/test
    defaults: { _controller: AcmeDemoBundle:Xmlrpc:test }

controller

public function indexAction()
{
    $server = new \Zend\XmlRpc\Server;
    $server->setClass($this->get('MyTestService'));

    $response = new Response();
    $response->headers->set('Content-Type', 'text/xml; charset=ISO-8859-1');
    ob_start();
    $server->handle();
    $response->setContent(ob_get_clean());
    return $response;
}
public function testAction()
{
    $client = new \Zend\XmlRpc\Client('`http://127.0.0.1/symfony_xmlrpc/web/app_dev.php/xmlrpc`');
    $result= $client->call('ping', array('test'));
    echo '<br/><br/>XmlRpc:<br/>';
    var_dump ( $result );

    $response = new Response();
    $response->headers->set('Content-Type', 'text');
    ob_start();

    $response->setContent('testme');
    return $response;

}

MyTestService

namespace Acme\DemoBundle\Controller;

class MyTestService {

    /**
     * A simple ping service
     *
     * @param string $value
     * @return string
     */
    function ping($value) {
        return $value . ' back from server symfony';
    }
    /**
     * A simple pong service
     *
     * @param string $token
     * @param array $arg
     * @return array
     */
    function pong($token, $arg) {
        return array($token.'data'=>$arg);
    }
}

OTHER TIPS

Xmlrpc (request send to external url to retrieve data) by using simple sudo apt-get install php5-xmlrpc, we can install xmlrpc. We can use it in symfony and php also accordimg to url format we have to request. In controller:

public function testAction()
{

    $method = 'index.bus';
    $date = '2002-02-20';
    $source = 112;
    $dest = 69;
    $s_id = 1310;
    $seat = 'seat';
    $request = xmlrpc_encode_request($method, array("date"=>$date,
        "sourceids"=>$source,
        "destinationids"=>$dest,
        "serviceids"=>$s_id,
        "selected_seat"=>$seat));

    $context = stream_context_create(array('http' => array(
      'method' => "POST",
      'header' => "Content-Type: text/xml\r\nUser-Agent: PHPRPC/1.0\r\n",
      'content' => $request
    )));

    //(external url) their format of request
    $file = file_get_contents("http://url.com/api/cabs/server.php? SecurityKey=ATSELKSWER", false, $context);
   $response = xmlrpc_decode($file);

    if ($response && xmlrpc_is_fault($response)) {
      trigger_error("xmlrpc:$response[faultString]($response[faultCode])");
    } 
    else{
        print_r($response);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top