1

I'm new developer, I need to connect XML API Hotel, In the documentation is mentioned about Startup guide, first we need to add customer login and user, there're two steps at first, but I don't know how to use them, and how to connect Main XML URL into PHP using the password login info like below:

General Request / Request Format:

    <customer>  
    <username>username</username>  
    <password>md5(password)</password>  
    <id>company code</id>  
    <source>1</source>  
    <request command="getallcities">  
        <return>  
            <filters>  
                <countryCode></countryCode>  
                <countryName></countryName>  
            </filters>  
            <fields>  
                <field>countryName</field>  
                <field>countryCode</field>  
            </fields>  
        </return>  
    </request>  
</customer> 

General Response / Response Format:

    <result command=" " date="" ip="">  
    <request>  
        <error>  
            <class></class>  
            <code>error code</code>  
            <details></details>  
            <extraDetails></extraDetails>  
            <postXml></postXml>  
        </error>  
        <successful>FALSE</successful>  
    </request>  
</result>  

General Request XSD

getallcities.xsd

I highly appreciate of someone will guide me how to use them and how to connect PHP into Main XML Url.

I highly appreciate your help in this issue.

Thanks in advance

  • Possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – ThW Nov 28 '16 at 09:15

1 Answers1

1

Okay, so, you didn't give very much to work with, but I'm going to try and help you solve this.

First off, let's use a PHP package to simplify converting an array into valid XML. It will save you time down the road. I recommend Spatie's Array to Xml package.

If you're familiar with composer, go ahead and install that. If you've never used composer, go ahead and download src/ArrayToXml.php and put it in your project. (I highly recommend you use composer if you don't, I'm just providing some extra instructions since I don't know your level of experience.)

Now, I'm going to assume you got the Array To Xml package set up. Let's go ahead and structure an array before we convert it to xml.

// Structure the payload as an array and pass in any data you need dynamically
$payload = [
  'username' => 'username',
  'password' => 'md5(password)',
  'id' => 'company code',
  'source' => '1',
  'request' => [
    'return' => [
      'filters' =>  [
        'countryCode' => [],
        'countryName' => [],
      ],
      'fields' => [
        'field' => [
          'countryName',
          'countryCode',
        ],
      ],
    ],
    '@attributes' => [
      'command' => 'getallcities',
    ],
  ],
];

// Convert the array to xml with a root of 'customer'
$xml = \Spatie\ArrayToXml\ArrayToXml::convert($payload, 'customer');

From there, we're going to initiate a curl call to the endpoint with your payload.

// Set the url you're making an api call to
$endpoint = 'https://yoursite.com/your-endpoint';

$curl = curl_init();

curl_setopt_array($curl, [
    CURLOPT_URL => $endpoint,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $xml,
    CURLOPT_HTTPHEADER => [
        "Content-Type: application/xml"
    ]
]);

$response = curl_exec($curl);
$error = curl_error($curl);

curl_close($curl);

if ($error) {
  echo "cURL Error #:" . $error;
} else {
  echo $response;
}

Now you'll want to parse the response xml back into an array so you can process it. I created a package to help you do this. You can find it here: https://github.com/mtownsend5512/xml-to-array

The basic premise of installing it is the same. You'll do $response = \Mtownsend\XmlToArray\XmlToArray::convert($response);

See how that works.

Mark
  • 1,255
  • 1
  • 13
  • 25