简体   繁体   English

如何在 Symfony 中配置 XML-RPC 服务器

[英]How to configure XML-RPC server in Symfony

In my website I need to configure the REST, XML-RPC, SOAP servers.在我的网站中,我需要配置 REST、XML-RPC、SOAP 服务器。

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?如何在 Symfony 中配置 Zend XmlRpc 服务器?

Any useful links with step by step configuration?任何有用的分步配置链接

Or any other XML-RPC which can be used with symfony或者任何其他可以与 symfony 一起使用的 XML-RPC

Thanks in advance, SVN提前致谢,SVN

I have used zend xmlrpc with symfony我在 symfony 中使用了 zend xmlrpc

composer.json作曲家.json

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

config.yml配置文件

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

routing.yml路由.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);
    }
}

Xmlrpc (request send to external url to retrieve data) by using simple sudo apt-get install php5-xmlrpc , we can install xmlrpc . Xmlrpc(请求发送到外部url以检索数据)通过使用简单的sudo apt-get install php5-xmlrpc ,我们可以安装xmlrpc We can use it in symfony and php also accordimg to url format we have to request.我们可以在 symfony 和 php 中使用它,也可以根据我们必须请求的 url 格式使用它。 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM