I am looking for some help to write a simple PHP function which will make request to website and get title from title tag and save the title in local file, if I make a request again for same link, I will get title from local file and display it.
I found this for a starting point. but I am very new to php. can someone help please. Thank you so much.
function website_title($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// some websites like Facebook need a user agent to be set.
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36');
$html = curl_exec($ch);
curl_close($ch);
$dom = new DOMDocument;
@$dom->loadHTML($html);
$title = $dom->getElementsByTagName('title')->item('0')->nodeValue;
$noaccess= array('404', '403');
$url_string = end(explode(' ',$title));
if (in_array($url_string,$noaccess)){
$title = $url;
} else
{
}
return $title;
}