php获得 http 变量值,php – 使用cURL获取HTTP标头并保存到变量

我正在使用它来获取XML提要和HTTP标头

// 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标头的一部分,即

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

并将其保存到变量,我该怎么做?我一直在浏览PHP手册,但无法解决

解决方法:

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

return $result[1];

标签:php,curl,http-headers

来源: https://codeday.me/bug/20190902/1793055.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值