0

I'm using forecast.io to provide weather data on my website. I get charged for every API call from my website.

The problem is when Google crawls my site, it generates an API call and I get charged for it. I'm trying to stop Googlebot from triggering the API call. This is what I've tried and it doesn't seem to prevent the API calls. I'm still receiving 3000 calls a day, from all the crawling activity.

<?php
if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot"))
{
echo "<h1>Today's weather</h1>";
}
else{
include("assets/php/forecast.php");
$api_key = '123123';

$latitude = $lat;
$longitude = $lon;
if ( $latitude !='NULL' && $longitude !='NULL'){
$forecast = new ForecastIO($api_key);
/*
* GET CURRENT CONDITIONS
*/
$condition = $forecast->getCurrentConditions($latitude, $longitude);
}
else {
$latitude = "-33.859972";
$longitude = "151.209444";
$forecast = new ForecastIO($api_key);
$condition = $forecast->getCurrentConditions($latitude, $longitude);
}
$temp = $condition->getTemperature();
$temp = $temp + 2;
$summ = $condition->getSummary();
$icon = $condition->getIcon();
$icon2 = $condition->getIcon();
$icon = str_replace('-', '_', $icon);
$icon2 = str_replace('-', ' ', $icon2);
$icon = strtoupper($icon);
$icon2 = ucfirst($icon2);
}
?>    

Currently <?php 
if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot"))
{
  $min=10;
  $max=40;
  echo rand($min,$max);
}
else 
{
echo $temp;
} 
?>°C 

Today: 
    <?php 
    if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot"))
    {
    echo "Pleasant";
    }
    else 
    {
    echo $summ;
    }  ?> day. 

Present weather: <?php
    if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot"))
    {
    echo "Pleasant";
}
else 
{
echo $icon2;
} ?></font>
<canvas id="icon1" width="40" height="40"></canvas>
<script>
  var skycons = new Skycons({"color": "black"});

  skycons.add("icon1", Skycons.<?php
if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot"))
{
echo "PARTLY_CLOUDY_NIGHT";
}
else 
{
echo $icon;
} ?>);

  skycons.play();

</script>
Onetest
  • 35
  • 1
  • 1
  • 8
  • Set a `Disallow: ` record in your site's `robots.txt`. Alternatively, check the user agent header for `googlebot` and don't allow API calls to be made when it matches. – esqew Jul 15 '14 at 15:14
  • Alternatively, check the user agent header for googlebot and don't allow API calls to be made when it matches - That's what i've tried to do with the code above, but my solution doesn't work – Onetest Jul 15 '14 at 15:17
  • Your protections look sufficient. Are you absolutely sure `googlebot` is the **only** one hitting your page? There are plenty of other spiders out there that do exactly the same thing. – esqew Jul 15 '14 at 15:22
  • hey that could be it, other crawlers.. how do I make it match all crawlers? – Onetest Jul 15 '14 at 15:23
  • The answers to [this SO question](http://stackoverflow.com/questions/677419/how-to-detect-search-engine-bots-with-php) may be useful to you. – esqew Jul 15 '14 at 15:24
  • thanks, i think i visited that answer before, but rereading it was useful – Onetest Jul 15 '14 at 15:32

0 Answers0