简体   繁体   English

使用CURL返回XML响应

[英]returning a XML response with CURL

I am passing some parameters via HTTP POST to a webserver. 我通过HTTP POST将一些参数传递给网络服务器。

I am getting a response, but ideally I want a full XML responses, whereas I seem to be only getting a concatenated string. 我得到了一个响应,但理想情况下我想要一个完整的XML响应,而我似乎只得到一个连接的字符串。 I've tried SimpleXMLElement, but it doesn't seem to do anything, and it isn't returning any XML. 我已经尝试过SimpleXMLElement,但它似乎没有做任何事情,并且它没有返回任何XML。

Below is my code: 以下是我的代码:

$post_string = '<?xml version="1.0" encoding="utf-16" ?> 
<ChameleonIAPI>
  <Method>TitleList</Method> 
  <APIKey>D12E9CF3-F742-47FC-97CB-295F4488C2FA</APIKey> 
  <UserName>David</UserName>
  <Filter>
  </Filter>
</ChameleonIAPI>';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"https://jobs.chameleoni.com/PostXML/PostXml.aspx");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
        "Action=postxml&AuthKey=Guest&AuthPassword=KgwLLm7TL6G6&Xml=$post_string");

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);

$server_output = curl_exec ($ch);

echo $server_output." TEST <br />";
curl_close ($ch);

$oXML = new SimpleXMLElement($server_output);

foreach($oXML->entry as $oEntry){
  echo $oEntry->title . "\n";
}

Here's the page which you can even test the POST and XML. 这是您甚至可以测试POST和XML的页面。

https://jobs.chameleoni.com/PostXML/PostingXML.html https://jobs.chameleoni.com/PostXML/PostingXML.html

My XML seems to be ok, as it works on the test page. 我的XML似乎没问题,因为它适用于测试页面。 I assume there is something wrong with my PHP, although I have no idea what that is! 我认为我的PHP有问题,虽然我不知道那是什么!

Any help would be lovely! 任何帮助都很可爱!

Your code retrieve full XML, to see it write: 您的代码检索完整的XML,看它写:

echo htmlentities( $server_output );
die();

and in the browser you'll see: 在浏览器中你会看到:

<?xml version="1.0" encoding="utf-16" ?> <ChameleonIAPIResponse> <?xml version =“1.0”encoding =“utf-16”?> <ChameleonIAPIResponse>

<Titles><TitleId>1</TitleId><Title></Title><TitleId>6</TitleId><Title>Mr</Title><TitleId>2</TitleId><Title>Mrs</Title><TitleId>3</TitleId><Title>Miss</Title><TitleId>4</TitleId><Title>Ms</Title><TitleId>5</TitleId><Title>Dr</Title><TitleId>43</TitleId><Title>Sir</Title></Titles> </ChameleonIAPIResponse> <标题> <TitleId> 1 </ TitleId> <TITLE> </ TITLE> <TitleId> 6 </ TitleId> <TITLE>先生</ TITLE> <TitleId> 2 </ TitleId> <TITLE>杜</ TITLE> <TitleId> 3 </ TitleId> <TITLE>小姐</ TITLE> <TitleId> 4 </ TitleId> <TITLE>女士</ TITLE> <TitleId> 5 </ TitleId> <TITLE>博士</ TITLE> <TitleId > 43 </ TitleId> <Title> Sir </ Title> </标题> </ ChameleonIAPIResponse>

The problem is that the browser interpret your output as HTML, so the tag are hidden (see at the browser page source, and you will find your complete XML). 问题是浏览器将您的输出解释为HTML,因此隐藏了标记(请参阅浏览器页面源,您将找到完整的XML)。

In addition, to send XML as XML, before you have to send appropriate headers: 此外,要在发送适当的标头之前将XML作为XML发送:

header( 'Content-type: text/xml' );
echo $server_output;
die();

No other output before and after above code, otherwise your XML will result broken. 在上面的代码之前和之后没有其他输出,否则你的XML将被破坏。

If you prefer SimpleXML, you can do this: 如果您更喜欢SimpleXML,您可以这样做:

$oXML = new SimpleXMLElement( $server_output );
header( 'Content-type: text/xml' );
echo $oXML->asXML();
die();

Also in this case, no output before and after (So comment previous lines). 同样在这种情况下, 之前和之后没有输出 (所以注释前面的行)。

You need to pass the Content-Type header with the value of application/x-www-form-urlencoded 您需要传递带有application/x-www-form-urlencoded值的Content-Type标头

Also, the xml payload needs to be urlencoded. 此外, xml有效负载需要进行urlencoded。

Example with $post_string being the same as in your question $post_string示例与您的问题中的相同

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"https://jobs.chameleoni.com/PostXML/PostXml.aspx");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS,
        "Action=postxml&AuthKey=Guest&AuthPassword=KgwLLm7TL6G6&Xml=" . urlencode($post_string));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);

$server_output = curl_exec ($ch);

curl_close ($ch);

var_dump($server_output);

Will output 会输出

string(374) "<?xml version="1.0" encoding="utf-16" ?>
<ChameleonIAPIResponse>

<Titles><TitleId>1</TitleId><Title></Title><TitleId>6</TitleId><Title>Mr</Title><TitleId>2</TitleId><Title>Mrs</Title><TitleId>3</TitleId><Title>Miss</Title><TitleId>4</TitleId><Title>Ms</Title><TitleId>5</TitleId><Title>Dr</Title><TitleId>43</TitleId><Title>Sir</Title></Titles>
</ChameleonIAPIResponse>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM