如何在http://www.example-webpage.com/file.html不使用的情况下获取html源代码file_get_contents()?
我需要知道这一点,因为在某些webhosts allow_url_fopen被禁用,所以你不能使用file_get_contents().是否可以使用cURL获取html文件的源代码(如果启用了cURL支持)?如果是这样,怎么样?谢谢.
The*_*can 37
请尝试以下方法:
$ch = curl_init("http://www.example-webpage.com/file.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)
我只推荐这个小文件.大文件作为一个整体读取,可能会产生内存错误.
编辑:在评论中进行一些讨论之后我们发现问题是服务器无法解析主机名,而且页面还有一个https资源,所以这里是你的临时解决方案(直到你的serveradmin修复名称解析).
我所做的只是ping graph.facebook.com以查看ip地址,通过ip地址替换主机名,而是手动提供标头.然而,这会使ssl证书无效,因此我们必须压制同行验证
//$url = "https://graph.facebook.com/19165649929?fields=name";
$url = "https://66.220.146.224/19165649929?fields=name";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: graph.facebook.com'));
$output = curl_exec($ch);
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)
请记住,ip地址可能会改变,这是一个错误来源.你应该使用curl_error()进行一些错误处理;