首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用CURL代替file_get_contents?

如何使用CURL代替file_get_contents?
EN

Stack Overflow用户
提问于 2011-12-17 06:19:38
回答 4查看 85.7K关注 0票数 39

我使用file_get_contents函数来获取和显示特定页面上的外部链接。

在我的本地文件中一切正常,但是我的服务器不支持file_get_contents函数,所以我尝试使用以下代码使用cURL:

代码语言:javascript
复制
function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

 echo file_get_contents_curl('http://google.com');

但它返回一个空白页。怎么啦?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-12-17 16:13:53

试试这个:

代码语言:javascript
复制
function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);       

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}
票数 85
EN

Stack Overflow用户

发布于 2011-12-24 01:11:21

这应该是可行的

代码语言:javascript
复制
function curl_load($url){
    curl_setopt($ch=curl_init(), CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

$url = "http://www.google.com";
echo curl_load($url);
票数 11
EN

Stack Overflow用户

发布于 2016-10-30 02:15:10

//你可以试试这个。它应该工作得很好。

代码语言:javascript
复制
function curl_tt($url){

$ch = curl_init();

curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);  
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);     
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data = curl_exec($ch);
curl_close($ch);

return $data;
}
echo curl_tt("https://google.com");
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8540800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档