Hello,
We have a Php function that retrieves the content of a Site for Indexing into our Search engine.
This works fine, when the Site responds in normal time of a second or two.
But sometimes the Site does not respond in a timely fashion, so in this case, say after 5 Seconds we would like this Php function given up and return “No results”.
you can see the sample page containing this Php function here:
https://www.anoox.com/temp/get_site_data.php
So if you go ahead and enter:
Anoox.com
Google.com
etc.
you will see that in a split second the function will return the selected Meta data of these Sites.
However, if you enter this Site for example:
http://www.deltapdpumps.com/retrofit-spares.php
you will see that the Php function cannot get this Site data, so it will just hang!.
So how do we tell the Php function to only wait for a reply from this Php function for only X Seconds and give up after that?
And here is the whole of these 2 Php functions that the above sample page POSTs to:
<?php
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);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function get_meta_data ($url) {
$meta_data_list = array();
$html = file_get_contents_curl("$url");
//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
ini_set('error_log', '/logs/specific_errors.php');
error_reporting( -1 ); // maximum errors, warnings, etc
ini_set( 'display_errors', false); // Save in log BUT not Show on Screen
$title = $nodes -> item(0) -> nodeValue;
$metas = $doc->getElementsByTagName('meta');
for ($i = 0; $i < $metas->length; $i++)
{
$meta = $metas->item($i);
if($meta->getAttribute('name') == 'description')
$description = $meta->getAttribute('content');
if($meta->getAttribute('name') == 'keywords')
$keywords = $meta->getAttribute('content');
}
if (isset($title)) {
array_push($meta_data_list, $title);
} else {
array_push($meta_data_list, null);
}
if (isset($description)) {
array_push($meta_data_list, $description);
} else {
array_push($meta_data_list, null);
}
if (isset($keywords)) {
$kw_list = explode(', ', $keywords);
if (count($kw_list) > 0) {
array_push($meta_data_list, $kw_list);
} else {
array_push($meta_data_list, null);
}
}
return $meta_data_list;
}//Closes get_meta_data
$site_data = array();
if (isset($_POST['url'])) {
$url = $_POST['url'];
$result = get_meta_data ($url);
if ($result) {
$site_data = $result;
} else {
$site_data = array_push($site_data, '<font color="red">Sorry but cannot access this Site</font>');
}
}
?>