2013-09-06 20 views
5

Possiedo un'applicazione con un'API RESTful dei servizi Web. Quando faccio richieste HTTP GET nel browser ottengo risposte XML.PHP XML CURL HTTP Formato XML

Quando faccio la stessa richiesta utilizzando PHP ottengo le informazioni corrette ma non è formattato in XML e quindi non posso passarlo a Simple XML.

Ecco il mio codice.

<?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); 

?> 

Qualsiasi pensiero o suggerimento sarebbe davvero utile.

S

EDIT:

Quindi questa è la risposta che ricevo da codice PHP:

0x100000rhel-mlsptrue9.2.3.0101 

Questa è la risposta se uso il WizTools Resto cliente o di un 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> 

Come si può vedere le informazioni sono lì, ma il PHP non è realmente presentare questa in modo utile.

risposta

0

Prova questa

$resp = explode("\n<?", $data); 
    $response = "<?{$resp[1]}"; 


    $xml = new SimpleXMLElement($response); 

Ha stampare qualsiasi cosa (il codice)? Prova a utilizzare echo $data ma premi F12 per visualizzare i risultati sulla console.

+0

Hmm non è sicuro di cosa sarebbe dovuto succedere ma utilizzando il codice ho appena ottenuto un'eccezione irreversibile. – Simon

+0

Ho trovato il motivo per cui non visualizza le informazioni correttamente e questo perché i tag nell'XML sono sconosciuti. Penso di aver bisogno di includere un file di schema in modo che sappia cosa fare con tutto questo. – Simon

7

Sono riuscito a trovare la risposta a questa domanda, quindi ho pensato di condividere il codice qui.

//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); 

Come si può vedere il codice non è poi così diverso, la cosa principale è che separa l'opzione porta fuori l'URL e nella propria opzione.

Speriamo che questo aiuti qualcun altro fuori !!!

S