PHP:如何使用curl而不是file_get_contents? [英] PHP : How to use curl instead file_get_contents?
本文介绍了PHP:如何使用curl而不是file_get_contents?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 file_get_contents
函数获取并显示我的特定页面上的外部链接。
好的。但是由于我的服务器不支持 file_get_contents
函数,所以我尝试使用cURL与下面的代码:
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');
但它返回一个空白页。
$ $<$ p $ p>
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;
}
I use file_get_contents
function to get and show external link on my specific page.
In local everything is okay. But as my server doesn't support file_get_contents
function, so I try to use cURL with the below code:
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');
But it returns me a blank page.
What is wrong?
解决方案
try this:
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;
}
这篇关于PHP:如何使用curl而不是file_get_contents?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文