gpt4 book ai didi

php - 如何优化此代码以提取标题

转载 作者:行者123 更新时间:2023-12-04 06:25:58 26 4
gpt4 key购买 nike

下面是我用来提取任何网站标题的示例代码:

function fread_url($url,$ref="")
{
if(function_exists("curl_init")){
$ch = curl_init();
$user_agent = "googlebot";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt( $ch, CURLOPT_HTTPGET, 1 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$html = curl_exec($ch);
curl_close($ch);
}
else{
$html.= file_get_contents($urweb);
}
return $html;
}
////////////////////////////////////
$doc = new DOMDocument(); @$doc->loadHTML(@fread_url($urweb));
$titlelist = $doc->getElementsByTagName("title");
if($titlelist->length > 0){ $wbtitle = $titlelist->item(0)->nodeValue; }
echo $wbtitle;

我的问题是如何修改此脚本以访问网站 5 秒钟,如果没有可用标题,则返回 ampty?现在对于某些网站,提取标题需要 5 秒以上,甚至需要更多时间。

最佳答案

为 cURL 设置超时。

curl_setopt($ch, CURLOPT_TIMEOUT, 5);

看起来您已经在尝试使用 CURLOPT_CONNECTTIMEOUT 做到这一点。 ,但那是

The number of seconds to wait while trying to connect



CURLOPT_TIMEOUT超时是

The maximum number of seconds to allow cURL functions to execute.



http://php.net/manual/en/function.curl-setopt.php

关于php - 如何优化此代码以提取标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6037395/

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