远程文件是一个附件,我可以获取标题,但我无法获取文件或内容。
<?php
error_reporting(E_ALL);
//not working, with rb too
//file_put_contents("natives.html", stream_get_contents(fopen("http://www.dev-c.com/nativedb/reference.html", 'r')));
//same with rb too
//file_put_contents("natives.html", fopen("http://www.dev-c.com/nativedb/reference.html", 'r'));
//same
//echo file_get_contents("http://www.dev-c.com/nativedb/reference.html");
//same
/*$url = 'http://www.dev-c.com/nativedb/reference.html';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, false);
curl_setopt($curl, CURLOPT_HEADER, true);
$data = curl_exec($curl);
curl_close($curl);
echo $data;*/
//same
$url = 'http://www.dev-c.com/nativedb/reference.html';
$file = basename($url);
$fp = fopen($file, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
因此,如果我使用这些代码中的任何一个,则输出一无所有,并且网络服务器上的创建文件为空。文件:http://www.dev-c.com/nativedb/reference.html
谢谢您的帮助,但问题在于编码。我必须发送标题"接受编码:gzip",然后gzdecode输出。
<?php
error_reporting(E_ALL);
$url = 'http://www.dev-c.com/nativedb/reference.html';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Host: www.dev-c.com",
"Accept-Encoding: gzip"
));
$data = curl_exec($curl);
curl_close($curl);
echo gzdecode($data);
?>
我拉出了您提供示例的Web URL的标题。这就是我得到的:
HTTP/1.1 200 OK
Server: nginx/1.12.0
Date: Fri, 23 Jun 2017 11:34:58 GMT
Content-Type: application/octet-stream
**Content-Length: 0**
Connection: keep-alive
Content-Description: reference.html
Content-Disposition: attachment; filename=reference.html
Content-Transfer-Encoding: binary
Expires: Thu Jun 22 11:34:58 2017 GMT
Cache-Control: must-revalidate
Pragma: public
Set-Cookie: PHPSESSID=f9d4eb515eeabfb461a11d056fdc7b78; path=/
Cache-Control: max-age=3600, private, proxy-revalidate
如您所见,内容长度:0为零。这可能是您获得空身体的原因之一。
如果您可以访问托管Reference.html的NGINX服务器,则可以尝试在尝试下载Referes reference.html时尝试丢弃/var/log/nginx/error.log的内容。
如果内容长度为零,则没有文件大小要下载。它指出了服务器设置的问题。