PHP cURL HTTP GET XML 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18662014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP cURL HTTP GET XML Format
提问by Simon
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.
我有一个具有 Web 服务 RESTful API 的应用程序。当我在浏览器中发出 HTTP GET 请求时,我会得到 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 发出相同的请求时,我得到了正确的信息,但它不是以 XML 格式设置的,因此我无法将其传递给 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:
所以这是我从 PHP 代码中得到的响应:
0x100000rhel-mlsptrue9.2.3.0101
This is the response if I use the WizTools Rest Client or a browser.
如果我使用 WizTools Rest Client 或浏览器,这就是响应。
<?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.
如您所见,信息已存在,但 PHP 并未真正以有用的方式呈现此信息。
回答by Simon
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.
正如您所看到的,代码并没有什么不同,主要是将端口选项从 URL 中分离出来并放入它自己的选项中。
Hopefully this helps someone else out!!!
希望这可以帮助其他人!!!
S
秒
回答by LefterisL
Try this
尝试这个
$resp = explode("\n<?", $data);
$response = "<?{$resp[1]}";
$xml = new SimpleXMLElement($response);
Does it print anything at all (your code)? Try using echo $data
but hit F12 to view the results on the console.
它是否打印任何内容(您的代码)?尝试使用echo $data
但按 F12 以在控制台上查看结果。