简体   繁体   English

适用于PHP 2的AWS S3 SDK-获取HTTP请求/响应字符串

[英]AWS S3 SDK for PHP 2 - Get HTTP Request/Response Strings

I have a generic HTTP file access API which I use for the system I'm working on. 我有一个用于正在使用的系统的通用HTTP文件访问API。 To make it as flexible as possible, it returns request and response data in the form of HTTP strings. 为了使其尽可能灵活,它以HTTP字符串的形式返回请求和响应数据。

I'm currently implementing a version which interacts with the S3, via the AWS SDK for PHP 2 . 我当前正在实现一个通过PHP 2AWS开发工具包与S3交互的版本。

Is there an easy way to quickly get the Request and Response HTTP requests which the S3Client makes when performing operations? 有没有一种简单的方法可以快速获取S3Client在执行操作时发出的请求和响应HTTP请求? If not, is there a more piecemeal way which I can use to not have to fake it? 如果没有,还有没有我可以使用的更零碎的方式呢?

Basically, I'd like the full-text of both the Request and Response on demand, or at least access to relevant data (headers, response codes, URLs, etc) so I can properly populate the return data for my framework. 基本上,我需要按需提供请求和响应的全文,或者至少要访问相关数据(标头,响应代码,URL等),以便可以为框架正确填充返回数据。

Thanks. 谢谢。

You can get either the request or response object from a command object. 您可以从命令对象获取请求或响应对象。 Assuming $s3 holds an instance of Aws\\S3\\S3Client , you could do something like this: 假设$s3拥有Aws\\S3\\S3Client的实例,则可以执行以下操作:

$command = $s3->getCommand('ListObjects', array('Bucket' => '<bucket-name>'));
$request = $command->getRequest();
$response = $command->getResponse();

Those objects have methods for viewing the body, headers, status codes, etc. and you can cast them to string to see the string form. 这些对象具有查看正文,标题,状态码等的方法,您可以将它们转换为字符串以查看字符串形式。

If you want to quickly see the request and response as you are executing commands, you can attach the wire logger, and see what comes out on STDOUT (or STDERR) 如果要在执行命令时快速查看请求和响应,则可以连接线记录器,并查看STDOUT(或STDERR)上显示的内容

$s3->addSubscriber(\Guzzle\Plugin\Log\LogPlugin::getDebugPlugin());
$s3->listObjects(array('Bucket' => '<bucket-name>'));

You will need to look into the Guzzle\\Http\\Client class, which is an ancestor class to S3Client , to have a look at the methods that it makes available. 您将需要查看Guzzle\\Http\\Client类,该类是S3Client的祖先类,以查看其可用的方法。 You can always override some of these methods in your own child of S3Client to make accessing this information easier for you. 您始终可以在自己的S3Client子级中覆盖其中一些方法,以使您更轻松地访问此信息。

Ultimately the data you are looking for resides in an object of class Guzzle\\Http\\Message\\Response , which I believe is returned from Guzzle\\Http\\Client::send() . 最终,您要查找的数据驻留在Guzzle\\Http\\Message\\Response类的对象中,我相信它是from Guzzle\\Http\\Client::send()

So perhaps in your own implementation of S3Client you can override the send() method to send the HTTP requests, then process the response data as needed. 因此,也许在您自己的S3Client实现中,您可以重写send()方法以发送HTTP请求,然后根据需要处理响应数据。

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

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