Categories

Social popularity

This example will show how many times your site is mentioned on sites like facebook, twitter and linkedin. The example uses google.

<?php
$url = "http://www.google.com";
$url = rawurlencode($url);
$json_string = file_get_contents_curl('http://urls.api.twitter.com/1/urls/count.json?url=' . $url);
$json = json_decode($json_string, true);
echo isset($json['count'])?intval($json['count']):0;

$json_string = file_get_contents_curl('http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls='.$url);
$json = json_decode($json_string, true);
echo $json[0]['total_count'] . "facebook likes+shares+comments";

$json_string = file_get_contents_curl("http://www.linkedin.com/countserv/count/share?url=$url&format=json");
$json = json_decode($json_string, true);
echo $json['count'] . " linkedin shares";

$json_string = file_get_contents_curl('http://www.stumbleupon.com/services/1.01/badge.getinfo?url='.$url);
$json = json_decode($json_string, true);
echo $json['result']['views'] . " Stumbleupon views";

$return_data = file_get_contents_curl('http://api.pinterest.com/v1/urls/count.json?url='.$url);
$json_string = preg_replace('/^receiveCount\((.*)\)$/', "\\1", $return_data);
$json = json_decode($json_string, true);
echo $json['count'] . " pins";

function file_get_contents_curl($url)
{
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$cont = curl_exec($ch);
if(curl_error($ch))
{
die(curl_error($ch));
}
return $cont;
}
?>

Alexa ranking

<?php
$url="beginnersphp.co.uk";
$xml = simplexml_load_file('http://data.alexa.com/data?cli=10&dat=snbamz&url='.$url);
$rank=isset($xml->SD[1]->POPULARITY)?$xml->SD[1]->POPULARITY->attributes()->TEXT:0;
$web=(string)$xml->SD[0]->attributes()->HOST;
echo $web." has an Alexa Rank of ".$rank;
?>

Get weather example

<?php
$city="Dundee";
$country="UK"; //Two digit country code
$url="http://api.openweathermap.org/data/2.5/weather?q=".$city.",".$country."&units=metric&cnt=7&lang=en";
$json=file_get_contents($url);
$data=json_decode($json,true);
//Get current Temperature in Celsius
echo "Temperature : " . $data['main']['temp']." c<br>";
//Get min current Temperature in Celsius
echo "Minimum Temperature : " . $data['main']['temp_min']." c<br>";
//Get max current Temperature in Celsius
echo "Maximum Temperature : " . $data['main']['temp_max']." c<br>";
//Get air pressure
echo "Air pressure : " . $data['main']['pressure']."<br>";
//Get humidity
echo "Humidity : " . $data['main']['humidity']."<br>";
//Get weather conditions
echo "Conditions : " . $data['weather'][0]['main']."<br>";
//Get cloud percentage
echo "Cloud percentage : " . $data['clouds']['all']." %<br>";
//Get wind speed
echo "Wind speed : " . $data['wind']['speed']." mph<br>";
//Get wind direction
echo "Wind direction : " . $data['wind']['deg']."<br>";
?>

Valid email address filter

<?php

$emailAddress = "my_email_test@mail server.com";

if(!filter_var($emailAddress, FILTER_VALIDATE_EMAIL))
{
    echo "Submitted address is faulty";
}
else
{
    echo "Address submitted successfully";
}

?>

Dynamic Title Tags

<title>
<?php
if (is_home()) 
{ 
echo bloginfo('name');
} 
elseif (is_404())
 {
echo '404 Not Found';
} 
elseif (is_category()) 
{
echo 'Category:'; wp_title('');
} 
elseif (is_search()) 
{
echo 'Search Results';
} 
elseif ( is_day() || is_month() || is_year() ) 
{
echo 'Archives:'; wp_title('');
} 
else 
{
echo wp_title('');
}
?>
</title>