在 php 中点击 url 时发送 xml 响应 [英] sending xml response when a url is hit in php
问题描述
当一个人点击这个链接时,就会给出一个回复
When one will hit this link then a response will be given which will like
public function sendResponse($type,$cause) {
$response = '<?xml version="1.0" encoding="utf-8"?>';
$response = $response.'<response><status>'.$type.'</status>';
$response = $response.'<remarks>'.$cause.'</remarks></response>';
return $response;
}
我正在从我的控制器文件中调用此方法并只回显该值.击球手会得到这个回应吗?
I am calling this method from my controller file and just echo the value. will the hitter will get this response?
<?php
......
......
echo $sendResponse($type,$cause);
?>
这个回声会引起用户的共鸣吗?
Will user will get resonse by this echo?
推荐答案
return
单独不会向客户端发送任何内容.如果您回显 sendResponse()
的结果,则是的,客户端将收到 XML:
return
alone won't sending anything to the client. If you are echoing the result of sendResponse()
then yes, the client will receive the XML:
echo sendResponse($type,$cause);
注意我从 sendResponse 调用中删除了 $
- 如果您使用 $
,PHP 将假定它是一个变量.
Notice I removed the $
from the sendResponse call - PHP will assume it's a variable if you use the $
.
建议添加一个标头来告诉客户端正在发送 XML 和编码,但这对于 XML 的传输不是必需的:
It's recommended to add a header to tell the client that XML is being sent and the encoding, but it's not essential for the transport of the XML:
header("Content-type: text/xml; charset=utf-8");
您可以在声明 XML 标头后使用连接字符 .
:
You can use the concatenation character .
after you declared the XML header:
public function sendResponse($type,$cause) {
$response = '<?xml version="1.0" encoding="utf-8"?>';
$response .= '<response><status>'.$type.'</status>';
$response = $response.'<remarks>'.$cause.'</remarks></response>';
return $response;
}
....
....
header("Content-type: text/xml; charset=utf-8");
echo sendResponse($type,$cause);
这篇关于在 php 中点击 url 时发送 xml 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!