php:使用 cURL 获取 html 源代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3592270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php: Get html source code with cURL
提问by John Paneth
How can I get the html source code of http://www.example-webpage.com/file.html
without using file_get_contents()
?
如何在http://www.example-webpage.com/file.html
不使用的情况下获取 html 源代码file_get_contents()
?
I need to know this because on some webhosts allow_url_fopen
is disabled so you can't use file_get_contents()
. Is it possible to get the html file's source with cURL (if cURL support is enabled)? If so, how?
Thanks.
我需要知道这一点,因为在某些虚拟主机上allow_url_fopen
已禁用,因此您无法使用file_get_contents()
. 是否可以使用 cURL 获取 html 文件的源代码(如果启用了 cURL 支持)?如果是这样,如何?谢谢。
回答by The Surrican
Try the following:
请尝试以下操作:
$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);
I would only recommend this for small files. Big files are read as a whole and are likely to produce a memory error.
我只会推荐这个用于小文件。大文件是整体读取的,很可能会产生内存错误。
edit: after some discussion in the comments we found out that the problem was that the servercouldnt resolve the host name and the page was in addition a https resource so here comes your temporary solution (until your serveradmin fixes the name resolving).
编辑:在评论中进行了一些讨论后,我们发现问题在于服务器无法解析主机名,并且页面另外是一个 https 资源,所以这里是您的临时解决方案(直到您的服务器管理员修复了名称解析)。
what i did is just pinging graph.facebook.com to see the ip adress, replace the hostname by the ip adress and instead give the header manually. this however renders the ssl certificate invalid so we have to supress peer verification
我所做的只是 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);
keep in mind that the ip adress might change and this is an eror source. you should as well do some error handling using curl_error();
请记住,IP 地址可能会改变,这是一个错误来源。你也应该使用 curl_error() 做一些错误处理;
回答by Brad
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
Source: http://www.christianschenk.org/blog/php-curl-allow-url-fopen/
来源:http: //www.christianschenk.org/blog/php-curl-allow-url-fopen/
回答by phidah
Try http://php.net/manual/en/curl.examples-basic.php:)
试试http://php.net/manual/en/curl.examples-basic.php:)
<?php
$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
As the documentation says:
正如文档所说:
The basic idea behind the cURL functions is that you initialize a cURL session using the curl_init(),then you can set all your options for the transfer via the curl_setopt(), then you can execute the session with the curl_exec() and then you finish off your session using the curl_close().
cURL 函数背后的基本思想是您使用 curl_init() 初始化 cURL 会话,然后您可以通过 curl_setopt() 设置传输的所有选项,然后您可以使用 curl_exec() 执行会话,然后您使用 curl_close() 结束您的会话。
回答by Ahmet Sina Ustem
I found a tool in Github that could possibly be a solution to this question. https://incarnate.github.io/curl-to-php/I hope that will be useful
我在 Github 中找到了一个可能可以解决这个问题的工具。https://incarnate.github.io/curl-to-php/我希望这会有用