gpt4 book ai didi

php - 为什么这个使用 CURL 的函数对某些 URL 有效但对其他 URL 无效?

转载 作者:可可西里 更新时间:2023-10-31 22:08:41 25 4
gpt4 key购买 nike

我正在用 PHP 编写一个网站,用于聚合来自其他各种网站的数据。我有一个函数“returnPageSource”,它接受一个 URL 并将该 URL 中的 html 作为字符串返回。

function returnPageSource($url){
$ch = curl_init();
$timeout = 5; // set to zero for no timeout

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // means the page is returned
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOUT_CONNECTTIMEOUT, $timeout); // how long to wait to connect
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // follow redirects
//curl_setopt($ch, CURLOPT_HEADER, False); // only request body

$fileContents = curl_exec($ch); // $fileContents contains the html source of the required website
curl_close($ch);

return $fileContents;
}

这适用于我需要的一些网站,比如 http://atensembl.arabidopsis.info/Arabidopsis_thaliana_TAIR/unisearch?species=Arabidopsis_thaliana_TAIR;idx=;q=At5g02310 ,但不适用于其他人,例如 http://www.bar.utoronto.ca/efp/cgi-bin/efpWeb.cgi?dataSource=Chemical&modeInput=Absolute&primaryGene=At5g02310&orthoListOn=0 .有人知道为什么吗?

更新

感谢您的回复。我已将我的用户代理更改为与我的浏览器相同(Firefox 3,可以正常访问网站),将超时更改为 0,但我仍然无法连接,但我可以收到一些错误消息。 curl_error() 给我错误“无法连接到主机”,和 curl_getinfo($ch, CURLINFO_HTTP_CODE);返回 HTTP 代码 0...两者都不是很有帮助。我也尝试过 curl_setopt($ch, CURLOPT_VERBOSE, 1);,但没有显示任何内容。有没有人有任何其他想法?

最终更新

我刚刚意识到我没有解释问题所在 - 我只需要输入我大学的代理设置(我使用的是大学的服务器)。之后一切正常!

最佳答案

你应该使用 curl_error()检查发生了哪个错误(如果有的话)

关于php - 为什么这个使用 CURL 的函数对某些 URL 有效但对其他 URL 无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/551650/

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