Phil Shotton
Phil Shotton

Reputation: 105

XML RPC connection using PHP

Has anyone ever created code to connect to Adestra.com's XML RPC API with PHP.

I have seen a couple of XML RPC examples on here, but none of them demonstrate how to send username and password authentication via headers. According to the Adestra Support:-

"We use HTTP basic authentication, which requires the username and password passed through as headers. Most XML-RPC clients will handle this for you by exposing a more convenient interface for supplying credentials. To ensure credentials are passed securely, please connect to the API over https (https://app.adestra.com/api/xmlrpc)."

I [think I] understand the basic process here, i.e. you use an XML RPC library to encode function calls and parameters into XML format, and get a reponse from the server, but how would you send the authentication?

Any help much appreciated.

Upvotes: 2

Views: 2728

Answers (1)

MrD
MrD

Reputation: 2481

First you have to download XMLRPC client library. This library is used to create an XMLRPC object which will communicate with ADESTRA API services.

Bellow is example of calling API contact.search method. The principle stays the same for other API methods:

//******* 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:[email protected]/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("[email protected]", "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

Upvotes: 2

Related Questions