gpt4 book ai didi

php - 如何使用 CURL 而不是 file_get_contents?

转载 作者:IT王子 更新时间:2023-10-29 01:06:37 26 4
gpt4 key购买 nike

我使用 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');

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

最佳答案

试试这个:

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?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8540800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com