php:使用cURL获取html源代码 [英] php: Get html source code with cURL

查看:115
本文介绍了php:使用cURL获取html源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取 http://www.example-webpage.com/file.html 的html源代码,而不使用 file_get_contents )

How can I get the html source code of http://www.example-webpage.com/file.html without using file_get_contents()?

我需要知道这一点,因为在某些网络托管 allow_url_fopen 您不能使用 file_get_contents()。是否可以获得html文件的源代码与cURL(如果cURL支持启用)?如果是,如何?
谢谢。

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.

推荐答案

尝试以下操作:

$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.

编辑:在评论中的一些讨论后,我们发现,问题是servercouldnt解析主机名和页面是另外一个https资源,这里是你的临时解决方案(直到你的serveradmin修复名字解析)。

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).

我做的只是pinging graph.facebook.com看到ip地址,替换主机名ip地址,并手动给予标题。这会使ssl证书无效,因此我们必须拒绝对等验证。

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

//$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); 

请记住ip地址可能更改,这是一个错误源。你也应该使用curl_error();

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();

这篇关于php:使用cURL获取html源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆