使用 PHP 存储 post 请求的结果
我目前正在使用一个 API,该 API 要求我们使用 post 请求将 xml 中的集合详细信息发送到他们的服务器。
那里没什么大不了的,但它不起作用,所以我想将发送的 xml 输出到 txt 文件,这样我就可以查看实际发送的内容!
我没有发布到 API,而是发布到一个名为 target 的文档,但其输出记录的 xml 似乎确实是错误的。 这是我的目标脚本,请注意,发布脚本发布了 3 个项目,因此正在编写的文件应该依次包含每个发布请求的详细信息。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// get the request data...
$payload = '';
$fp = fopen('php://input','r');
$output_file = fopen('output.txt', 'w');
while (!feof($fp)) {
$payload .= fgets($fp);
fwrite($output_file, $payload);
}
fclose($fp);
fclose($output_file);
?>
我也尝试了以下操作,但这只是记录了最后一个发布请求,因此 txt 文件中只记录了 1 个集合项目,而不是全部 3 个
output_file = fopen('output.txt', 'w');
while (!feof($fp)) {
$payload .= fgets($fp);
}
fwrite($output_file, $payload);
fclose($fp);
fclose($output_file);
我知道我错过了一些非常明显的东西,但我整个早上都在看这个!
Im currently working with an API which requires we send our collection details in xml to their server using a post request.
Nothing major there but its not working, so I want to output the sent xml to a txt file so I can look at actually whats being sent!!
Instead of posting to the API im posting to a document called target, but the xml its outputting its recording seems to be really wrong. Here is my target script, note that the posting script posts 3 items, so the file being written should have details of each post request one after the other.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// get the request data...
$payload = '';
$fp = fopen('php://input','r');
$output_file = fopen('output.txt', 'w');
while (!feof($fp)) {
$payload .= fgets($fp);
fwrite($output_file, $payload);
}
fclose($fp);
fclose($output_file);
?>
I also tried the following, but this just recorded the last post request so only 1 collection item was recorded in the txt file, instead of all 3
output_file = fopen('output.txt', 'w');
while (!feof($fp)) {
$payload .= fgets($fp);
}
fwrite($output_file, $payload);
fclose($fp);
fclose($output_file);
I know im missing something really obvious, but ive been looking at this all morning!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改
为
另外,更改
为
Change
to
Also, change
to
您可能应该使用curl来获取内容,然后通过fwrite写入
you should probably use curl instead to fetch the content and then write it via fwrite
改成
change
to