Jump to content

php script to check if url is indexed


ianhaney

Recommended Posts

Hello

 

I am struggling with some coding, I need a script to check if a url stored in my database is indexed in Google and if it is to display a green check image and if it is not then to display a red cross image, below is the code I have that I found by doing some searching

function indexed($url) {
    $url = 'http://webcache.googleusercontent.com/search?q=cache:' . urlencode($url);
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Chrome 10');
    
    if (!curl_exec($ch)) {
        // var_dump('failed');
        return false;
    }
    
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    // var_dump($code);
    return $code == '200';
}

below is the whole code I have but is displaying an error and all the urls pulled from the database have a red cross but am sure they are indexed in Google

ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);

// connect to the database
include('connect-db.php');

function indexed($url) {
    $url = 'http://webcache.googleusercontent.com/search?q=cache:' . urlencode($url);
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Chrome 10');
    
    if (!curl_exec($ch)) {
        // var_dump('failed');
        return false;
    }
    
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    // var_dump($code);
    return $code == '200';
}

// get the records from the database
if ($result = $mysqli->query("SELECT id, category, url, DATE_FORMAT(domain_expiry_date, '%d/%m/%Y') AS domain_expiry_date, site_login_url, site_username_login, site_password_login, no_of_posts, tf, cf, domain_name_owner, DATE_FORMAT(domain_owner_dob, '%d/%m/%Y') AS domain_owner_dob, domain_owner_address, domain_owner_email, domain_owner_phone, email_account, email_account_address, email_username, email_password, registrar, registrar_username, registrar_password, hosting_company, hosting_username, hosting_password, DATE_FORMAT(hosting_expiry_date, '%d/%m/%Y') AS hosting_expiry_date, hosting_cpanel_url, hosting_cpanel_username, hosting_cpanel_password, sites_linked_out_to, DATE_FORMAT(last_post_date, '%d/%m/%Y') AS last_post_date FROM websites"))

{
// display records if there are records to display
if ($result->num_rows > 0)
{
	
// display records in a table
echo "<table class='records'>";

// set table headers
echo '<tr>
<th>URL</th>
<th>Indexed</th>

</tr>';

while ($row = $result->fetch_object())
{
	
	$index = indexed($row->url);
	
	// set up a row for each record
echo "<tr>";
echo "<td class='expand'><a href='http://" . $row->url . "' target='_blank'>" . $row->url . "</a></td>";
echo "<td class='shrink'><img src='images/" . (($index->indexed) ? 'green-check.png' : 'red-cross.png') . "' style='width: 30px; height: 30px;'></td>";
echo "</tr>";
}

echo "</table>";

// retrieve and show the data 

}

// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}

}

// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}

// close database connection
$mysqli->close();

Notice: Trying to get property of non-object in /home/cloudnetworkhero/public_html/network/websites-info/indexed.php on line 54

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.