使用 CakePHP 通过 Put 发送 XML
我想向一个 API 发送一个 put 请求,该 API 想要将请求的详细信息作为 XML
显然,在将 PUT 与 PHP 结合使用时,我需要将 xml 作为文件发送。
我该怎么做?
这就是我正在尝试的:
$HttpSocket = new HttpSocket();
$result = $HttpSocket->put($put, $fh);
其中 $put 是 url,$fh 是我像这样动态创建的文件
$xmlObject = Xml::fromArray($xmlArray);
$xmlString = $xmlObject->asXML();
$fh = fopen('php://memory', 'rw');
fwrite($fh, $xmlString);
rewind($fh);
I want to send a put request to an API which wants the details of the request as XML
Apparently I need to send the xml as a file when using PUT with PHP.
How can I do this?
Here is what I'm trying:
$HttpSocket = new HttpSocket();
$result = $HttpSocket->put($put, $fh);
where $put is the url and $fh is a file i have made on the fly like this
$xmlObject = Xml::fromArray($xmlArray);
$xmlString = $xmlObject->asXML();
$fh = fopen('php://memory', 'rw');
fwrite($fh, $xmlString);
rewind($fh);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 cake 2.0.5 上测试过它,HttpSocket::put 可以发送键值数组或原始字符串作为 postdata。
因此,您可以直接发送 xml 字符串,远程服务器将在原始帖子数据中读取它,即
file_get_contents("php://input")
这有效:
为了演示它,我创建了在
'Controllers/RequestXmlTestController.php'
下归档一个名为 RequestXmlTestController 的控制器(代码如下),在'RequestXmlTests/index.ctp'
下归档一个空视图控制器代码:
参考:
HttpSocket 文档
I've tested it on cake 2.0.5, and HttpSocket::put can send key-value arrays or raw strings as postdata.
So, you can send the xml string directly, and the remote server will read it in Raw Post Data i. e.
file_get_contents("php://input")
This works:
To demonstrate it, I've created a Controller called RequestXmlTestController filed under
'Controllers/RequestXmlTestController.php'
(code is below), and an empty view filed under'RequestXmlTests/index.ctp'
Controller code:
References:
HttpSocket Documentation
我最终只使用 php 而不是 php 助手来完成此操作,
为了整洁,我更喜欢使用 Cake 类,但这适用于我正在使用的 api。
I ended up doing this just using php rather than the php helpers
I'd have preferred using the Cake classes for neatness, but this works with the api i was using.