在 php 中点击 url 时发送 xml 响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13471014/
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
sending xml response when a url is hit in php
提问by Mofizur
I would like to send a xml response when a url is hit with parameters like http://gmail.com/cb/send/send.php?user_name=admin1&password=test123&subscriber_no=1830070547&mask=Peter&sms='Test SMS'
当使用http://gmail.com/cb/send/send.php?user_name=admin1&password=test123&subscriber_no=1830070547&mask=Peter&sms='Test SMS'等参数命中 url 时,我想发送 xml 响应
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?
用户会得到这个回声的回应吗?
回答by MrCode
returnalone won't sending anything to the client. If you are echoing the result of sendResponse()then yes, the client will receive the XML:
return单独不会向客户端发送任何东西。如果您回应sendResponse()then yes的结果,客户端将收到 XML:
echo sendResponse($type,$cause);
Notice I removed the $from the sendResponse call - PHP will assume it's a variable if you use the $.
注意我$从 sendResponse 调用中删除了- 如果您使用 .php 文件,PHP 将假定它是一个变量$。
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:
建议添加一个标头来告诉客户端正在发送 XML 和编码,但这对于 XML 的传输不是必需的:
header("Content-type: text/xml; charset=utf-8");
You can use the concatenation character .after you declared the XML header:
您可以.在声明 XML 标头后使用连接字符:
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);
回答by Ivo
You just need to specify some header within your script which tells the browser/client to handle this content properly! Other than that you have to concatenate rather than reassign your $response var ;)
您只需要在脚本中指定一些标题,告诉浏览器/客户端正确处理此内容!除此之外,您必须连接而不是重新分配您的 $response var ;)
In php this should do the job:
在 php 这应该做的工作:
header("Content-type: text/xml;charset=utf-8");

