PHP cURL HTTP GET XML格式 [英] PHP cURL HTTP GET XML Format

查看:206
本文介绍了PHP cURL HTTP GET XML格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序具有Web服务RESTful API。当我在浏览器中发出HTTP GET请求时,我收到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.

当我使用PHP发出同样的请求时,我获得正确的信息,因此我无法将其传递给简单XML。

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.

这是我的代码。

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

编辑:

这是我从PHP代码得到的响应:

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

0x100000rhel-mlsptrue9.2.3.0101

这是如果我使用WizTools的Rest客户端或浏览器的响应。

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>

正如你可以看到的信息是有的,但PHP不是真正地以一个有用的方式呈现。

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

推荐答案

我能够找到这个问题的答案,所以我想我会在这里分享代码。

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

这篇关于PHP cURL HTTP GET XML格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆