I have been working on a way to trying and retrieve data using the xmlrpc but keep running into formatting issues on the output. Listed below I have included my php code the xml schema and other related info. As you will see I used preg_replace but thinking about it im not sure that the correct solution.
<?php
error_reporting(0);
$auth = $_POST ['auth'];
$user = $_POST ['user'];
$request = construct_xml($auth, $authuser, $user);
$response = check_availability($request);
$available = parse_response($response);
if ($available == 0) {
echo $response = preg_replace('#\s(?=alias:|login:|password:)#', '<br/>', $response);
}
else {
echo $response = preg_replace('#\s(?=Alias:|Login:|Password:)#', '<br/>', $response);
}
function construct_xml ($auth, $authuser, $user) {
return <<<XML
<?xml version='1.0' encoding='utf-8' ?>
<rpc module='Email' method='List_Mailboxes' version='1.0'>
<auth>
<username>$authuser</username>
<password>$auth</password>
</auth>
<username datatype='username'>$user</username>
</rpc>
XML;
}
function check_availability ($request) {
$header .= "POST / HTTP/1.0\r\n";
$header .= "Content-Type: text/xml\r\n";
$header .= "Content-Length: " . strlen($request) . "\r\n\r\n";
$fp = fsockopen("xmlrpc.******.co.uk", 29***, $errno, $errstr, 30);
if ($fp) {
fputs ($fp, $header . $request);
while (!feof($fp)) {
$response .= fgets($fp, 2316);
}
fclose ($fp);
$pos = strpos($response, "\r\n\r\n");
$response = substr($response, $pos + 4);
}
return $response;
}
function parse_response ($xml) {
$xml = new SimpleXMLElement($xml);
$code = $xml->response['code'];
$text = $xml->response['text'];
if ($code != 0) {
echo "Error occurred: $text\n";
exit;
}
return $xml->availability;
}
?>
And here is the response example:
<?xml version="1.0" encoding="utf-8"?>
<rpc request_id="2316">
<response code="0" text="OK"/>
<mailbox datatype="list">
<alias datatype="mailbox_name">qux</alias>
<login datatype="mailbox_login">qux@baz</login>
<password datatype="password">2fbAcx6E</password>
</mailbox>
<mailbox datatype="list">
<alias datatype="mailbox_name">quux</alias>
<login datatype="mailbox_login">quux@baz</login>
<password datatype="password">iAgw1R8z</password>
</mailbox>
</rpc>
And this is the schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="rpc">
<xs:complexType>
<xs:all>
<xs:element name="auth">
<xs:complexType>
<xs:all>
<xs:element name="username">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9_\-\.]{1,16}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9_\-\.\?\!\@\$\(\)]{5,16}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name="validate_only" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:boolean">
<xs:attribute name="datatype">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="bool"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="username" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="datatype">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="username"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:all>
<xs:attribute name="module" type="xs:string"/>
<xs:attribute name="version" type="xs:string"/>
<xs:attribute name="method" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:schema>
Is anyone able to assist with getting the desired output in a nice formatted way. I am new at this and all the php code above I constructed by studying the php.net site so I have no idea if its correct or not.
Thanks in advance