我有一个PHP脚本,它通过API向Amazon的Cloudfront提交无效请求。
我可以捕获响应,但返回的文本基本上如下所示:
HTTP/1.0 201 Created
Content-Type: text/xml
Location: https://cloudfront.amazonaws.com/2012-07-01/distribution/distribution ID/invalidation/invalidation ID
<Invalidation xmlns="http://cloudfront.amazonaws.com/doc/2012-07-01/">
<Id>IDFDVBD632BHDS5</Id>
<Status>InProgress</Status>
<CreateTime>2013-04-16T19:37:58Z</CreateTime>
<InvalidationBatch>
<Paths>
<Items>
<Path>/image1.jpg</Path>
</Items>
</Paths>
<CallerReference>20130416090001</CallerReference>
</InvalidationBatch>
</Invalidation>我基本上只想获取状态值,我想我可以通过正则表达式或一些字符串操作来实现,但我假设有一种更好的方法将返回的数据转换为对象并正确地访问它。
我试过了:
$dom = new DOMDocument();
$dom->loadXML($data);但是$data不能工作,因为它包含标题部分“HTTP/1.0201...”
有谁知道正确的处理方法吗?
发布于 2013-04-19 00:10:58
您使用的是什么脚本?您是否考虑过使用AWS SDK for PHP (https://github.com/aws/aws-sdk-php)?
使用AWS SDK for PHP,您可以执行以下操作:
// Instantiate a CloudFront client
$cloudfront = \Aws\CloudFront\CloudFrontClient::factory(array(
'key' => 'your-aws-access-key-id',
'secret' => 'your-aws-secret-key',
));
// Get the status of an invalidation
$invalidationStatus = $cloudfront->getInvalidation(array(
'DistributionId' => 'your-distribution-id',
'Id' => 'your-invalidation-id',
))->get('Status');发布于 2013-04-18 03:03:02
通常,Http客户端库应该做到这一点,您没有提到使用的那个。
不过,解析响应应该很简单,如下所示:
$dom->loadXML(substr($data, strpos("\n\n", $data)+2))发布于 2013-06-03 21:41:12
通过执行以下操作来解决此问题。
$resp = substr($resp, strpos($resp, "\r\n\r\n")+4); // This strips out the header
$outputxml = simplexml_load_string($resp); // Converts it to an object in PHP which can be fed back more easilyhttps://stackoverflow.com/questions/16067720
复制相似问题