我正在尝试使用PHP从CURL获取响应和响应标头,特别是对于Content- Disposition:因此我可以返回标题中传递的文件名。在curl_getinfo中似乎没有得到返回。
我尝试使用HeaderFunction调用函数以读取其他标头,但是,我无法将内容添加到数组中。
请问有人有什么想法吗?
以下是我的代码的一部分,该代码是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); ... function 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\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; }