【问题标题】:XML RPC connection using PHP使用 PHP 的 XML RPC 连接
【发布时间】:2014-05-14 22:54:22
【问题描述】:

有没有人创建过代码来使用 PHP 连接到 Adestra.com 的 XML RPC API。

我在这里看到了几个 XML RPC 示例,但没有一个演示如何通过标头发送用户名和密码验证。根据 Adestra 支持:-

“我们使用 HTTP 基本身份验证,这需要将用户名和密码作为标头传递。大多数 XML-RPC 客户端会通过提供更方便的接口来提供凭据来为您处理此问题。为确保安全地传递凭据,请连接通过 https (https://app.adestra.com/api/xmlrpc) 发送到 API。”

我[想我]了解这里的基本过程,即您使用XML RPC库将函数调用和参数编码为XML格式,并从服务器获得响应,但是您将如何发送身份验证?

非常感谢任何帮助。

【问题讨论】:

    标签: php xml rpc


    【解决方案1】:

    首先您必须下载 XMLRPC 客户端库。该库用于创建一个 XMLRPC 对象,该对象将与 ADESTRA API 服务进行通信。

    下面是调用 API contact.search 方法的例子。其他 API 方法原理相同:

    //******* LOGIN DATA*******/
    $account = 'account';
    $username = 'username';
    $password = 'password';
    $adestraCoreTable=1;
    
    
    /**INITIALIZE API*****/
    require_once('xmlrpc.inc');//First inlcude XMLRPC client library
    
    
    //Calling Adestra API with our credentials
    $xmlrpc= new xmlrpc_client("http://$account.$username:$password@new.adestra.com/api/xmlrpc");
    $xmlrpc->setDebug(0);
    $xmlrpc->request_charset_encoding="UTF-8";
    
    
    $msg = new xmlrpcmsg(
                        "contact.search",
                        array(
                            //Set user id
                            new xmlrpcval($adestraCoreTable, "int"),
                            new xmlrpcval(
                                array(
                                    "email"=> new xmlrpcval("test@test.com", "string"),
                                ),"struct"
                            )
                        )
    
                    );
    $response = $xmlrpc->send($msg);//Send request, and get the response
    
    
    if($response->faultCode()){//API call not succeed. This can happen when there is no connection
        $errorMessage=htmlspecialchars($response->faultString());
        $errorCode=htmlspecialchars($response->faultCode());
    }
    
    $returnValue = php_xmlrpc_decode($response->value());//Parse API return
    if(empty($returnValue)){//If return value is empty, user not find in Adestra, we should log that
        $errorMessage="Searching user by mail did not succeed! Problably there is no this user in Adestra DB";
        continue;///SCRIPT MUST continue for other users
    }
    $adestraUSERID=$returnValue[0]["id"];//Use this ID for what ever you want
    

    【讨论】:

    • 非常感谢,我想知道上面数组中` "email"` key=>val 后面的逗号, 是故意的还是错字?也许这在执行过程中造成了一些问题。
    • 可选,您可以删除逗号,但不会导致任何错误
    • “XMLRPC 客户端库”是指:gggeek.github.io/phpxmlrpc
    • 嗨@EfficionDave,我不确定(这是 5 年前),但我认为这是您链接的库。
    猜你喜欢
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 2014-01-27
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    相关资源
    最近更新 更多