使用cURL获取HTTP头并保存到变量 [英] use cURL to get HTTP header and save to variable

查看:860
本文介绍了使用cURL获取HTTP头并保存到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用它来抓取XML Feed和HTTP标头

I am using this to grab a XML feed and the HTTP headers

// Initiate the curl session
$ch = curl_init();

// Set the URL
curl_setopt($ch, CURLOPT_URL, $url);

// Allow the headers
curl_setopt($ch, CURLOPT_HEADER, true);

// Return the output instead of displaying it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the curl session
$output = curl_exec($ch);

// Close the curl session
curl_close($ch);

// Cache feed
file_put_contents($filename, $output);

// XML to object
$output = simplexml_load_string($output);

// Return the output as an array
return $output;

并返回这些标题

HTTP/1.1 200 OK
Cache-Control: public, max-age=30
Content-Length: 5678
Content-Type: text/xml
Expires: Tue, 22 Nov 2011 15:12:16 GMT
Last-Modified: Tue, 22 Nov 2011 15:11:46 GMT
Vary: *
Server: Microsoft-IIS/7.0
Set-Cookie: ASP.NET_SessionId=1pfijrmsqndn5ws3124csmhe; path=/; HttpOnly
Data-Last-Updated: 11/22/2011 15:11:45
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 22 Nov 2011 15:11:46 GMT

我只想让它返回HTTP标头的一部分,即

I only want it to return one part of the HTTP header, which is

Expires: Tue, 22 Nov 2011 15:12:16 GMT

并将其保存到变量,我该如何做?我已经查看PHP手册,但无法解决这个问题。

and save that to a variable, how do I do this? I have been looking through the PHP manual but can't work it out

推荐答案

preg_match('~^(Expires:.+?)$~ism', $headers, $result);

return $result[1];

这篇关于使用cURL获取HTTP头并保存到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆