[英]php curl download file and get header
i want to download file via curl in php also get headers of that. 我想通过curl在php中下载文件,也获得标题。
// handler
$h = fopen($filePath , 'w');
// curl
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_FILE, $h);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'getAllHeaders'));
$a = curl_exec($ch); // return false;
curl_close($ch);
fclose($h);
public function getAllHeaders($ch, $header)
{
// check for 200 ok
if (preg_match('/HTTP[\/\.0-9\s\t]+200[\s\t]+OK/i', $header)) {
// header 200 found
$this->header200 = true;
}
// grab all headers keys and values
preg_match('/^(?P<key>[a-z0-9\-]+)\:(?P<value>.*)$/i', $header, $matches);
// cycle
foreach ($matches as $key => $value) {
if (!is_numeric($key)) {
$this->headers[strtolower(trim($key))] = trim($value);
}
}
// headers
return $this->headers;
}
but i cant get headers and file not be downloaded. 但我无法获取标题,并且无法下载文件。 how can i solve that. 我该如何解决。
The following constraints apply to getAllHeaders() which you do not satisfy: 以下约束适用于您不满足的getAllHeaders():
The header data must be written when using this callback function. 使用此回调函数时,必须写入标头数据。 Return the number of bytes written. 返回写入的字节数。
How about: 怎么样:
public function getAllHeaders($ch, $header)
{
// store header here (how about storing in a field that is an array of headers?)
return strlen($header);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.