使用Curl将标题作为数组返回 [英] Returning header as array using Curl

查看:116
本文介绍了使用Curl将标题作为数组返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想得到回应&来自CURL的响应头使用PHP,专门用于Content-Disposition:attachment;所以我可以返回在标题中传递的文件名。这似乎没有在curl_getinfo中返回。



我已经尝试使用HeaderFunction调用一个函数来读取额外的标题,但是,我无法添加













b

下面是我的代码的一部分,它是一个Curl包装类:

  ... 
curl_setopt $ this-> _ch,CURLOPT_URL,$ this-> _url);
curl_setopt($ this-> _ch,CURLOPT_HEADER,false);
curl_setopt($ this-> _ch,CURLOPT_POST,1);
curl_setopt($ this-> _ch,CURLOPT_POSTFIELDS,$ this-> _postData);
curl_setopt($ this-> _ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ this-> _ch,CURLOPT_USERAGENT,$ this-> _userAgent);
curl_setopt($ this-> _ch,CURLOPT_HEADERFUNCTION,'readHeader');

$ this-> _response = curl_exec($ this-> _ch);
$ info = curl_getinfo($ this-> _ch);
...


函数readHeader($ ch,$ header)
{
array_push($ this-> _headers,$ header);
}


解决方案

  curl_setopt($ this-> _ch,CURLOPT_URL,$ this-> _url); 
curl_setopt($ this-> _ch,CURLOPT_HEADER,1);
curl_setopt($ this-> _ch,CURLOPT_RETURNTRANSFER,1);

$ response = curl_exec($ this-> _ch);
$ info = curl_getinfo($ this-> _ch);

$ headers = get_headers_from_curl_response($ response);

function get_headers_from_curl_response($ response)
{
$ headers = array();

$ header_text = substr($ response,0,strpos($ response,\r\\\
\r\\\
));

foreach(explode(\r\\\
,$ header_text)as $ i => $ line)
if($ i === 0)
$ headers ['http_code'] = $ line;
else
{
list($ key,$ value)= explode(':',$ line);

$ headers [$ key] = $ value;
}

return $ headers;
}


I'm trying to get the response & the response headers from CURL using PHP, specifically for Content-Disposition: attachment; so I can return the filename passed within the header. This doesn't seem to get returned within curl_getinfo.

I've tried using the HeaderFunction to call a function to read the additional headers, however, I am unable to add the contents to an array.

Does anyone have any ideas please?


Below is part of my code which is a Curl wrapper class:

 ...
 curl_setopt($this->_ch, CURLOPT_URL, $this->_url);
 curl_setopt($this->_ch, CURLOPT_HEADER, false);
 curl_setopt($this->_ch, CURLOPT_POST, 1);
 curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $this->_postData);
 curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($this->_ch, CURLOPT_USERAGENT, $this->_userAgent);
 curl_setopt($this->_ch, CURLOPT_HEADERFUNCTION, 'readHeader');

 $this->_response = curl_exec($this->_ch);
 $info = curl_getinfo($this->_ch);
 ...


 function readHeader($ch, $header)
 {
      array_push($this->_headers, $header);
 }

解决方案

Here, this should do it:

curl_setopt($this->_ch, CURLOPT_URL, $this->_url);
curl_setopt($this->_ch, CURLOPT_HEADER, 1);
curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($this->_ch);
$info = curl_getinfo($this->_ch);

$headers = get_headers_from_curl_response($response);

function get_headers_from_curl_response($response)
{
    $headers = array();

    $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));

    foreach (explode("\r\n", $header_text) as $i => $line)
        if ($i === 0)
            $headers['http_code'] = $line;
        else
        {
            list ($key, $value) = explode(': ', $line);

            $headers[$key] = $value;
        }

    return $headers;
}

这篇关于使用Curl将标题作为数组返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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