Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP cURL HTTP GET XML Format

Tags:

php

curl

xml

I have an application that has a Web Services RESTful API. When I make HTTP GET requests in the browser I get XML responses back.

When I make the same request using PHP I get the correct information but it is not formatted in XML and so I can't pass it to Simple XML.

Here's my code.

<?php
//Deifne user credentials to use with requests
        $user = "user";
        $passwd = "user";

        //Define header array for cURL requestes
        $header = array('Contect-Type:application/xml', 'Accept:application/xml');

        //Define base URL
        $url = 'http://192.168.0.100:8080/root/restful/';

        //Define http request nouns
        $ls = $url . "landscapes";

        //Initialise cURL object
        $ch = curl_init();

        //Set cURL options
        curl_setopt_array($ch, array(
            CURLOPT_HTTPHEADER => $header, //Set http header options
            CURLOPT_URL => $ls, //URL sent as part of the request
            CURLOPT_HTTPAUTH => CURLAUTH_BASIC, //Set Authentication to BASIC
            CURLOPT_USERPWD => $user . ":" . $passwd, //Set username and password options
            CURLOPT_HTTPGET => TRUE //Set cURL to GET method
        ));

        //Define variable to hold the returned data from the cURL request
        $data = curl_exec($ch);

        //Close cURL connection
        curl_close($ch);

        //Print results
        print_r($data);

?>

Any thoughts or suggestions would be really helpful.

S

EDIT:

So this is the response I get from the PHP code:

0x100000rhel-mlsptrue9.2.3.0101

This is the response if I use the WizTools Rest Client or a browser.

<?xml version="1.0" encoding="UTF-16"?>
<landscape-response total-landscapes="1" xmlns="http://www.url.com/root/restful/schema/response">
    <landscape>
        <id>0x100000</id>
        <name>rhel-mlsp</name>
        <isPrimary>true</isPrimary>
        <version>9.2.3.010</version>
    </landscape>
</landscape-response>

As you can see the information is there but the PHP is not really presenting this in a useful way.

like image 695
Simon Avatar asked Sep 06 '13 16:09

Simon


1 Answers

I was able to find the answer to this question so I thought I would share the code here.

//Initialise curl object
$ch = curl_init();

//Define curl options in an array
$options = array(CURLOPT_URL => "http://192.168.0.100/root/restful/<URI>",
    CURLOPT_PORT => "8080",
    CURLOPT_HEADER => "Content-Type:application/xml",
    CURLOPT_USERPWD => "<USER>:<PASSWD>",
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    CURLOPT_RETURNTRANSFER => TRUE
);

//Set options against curl object
curl_setopt_array($ch, $options);

//Assign execution of curl object to a variable
$data = curl_exec($ch);

//Close curl object
curl_close($ch);

//Pass results to the SimpleXMLElement function
$xml = new SimpleXMLElement($data);

print_r($xml);

As you can see the code is not all that different, the main thing was separating the port option out of the URL and into its own option.

Hopefully this helps someone else out!!!

S

like image 107
Simon Avatar answered Sep 20 '22 17:09

Simon