AJAX Archives
Since we’ve already figured out how to create TinyURL URLs remotely using PHP, we may as well create a small Ajax-enabled tiny URL creator. Using MooTools to do so is almost too easy.
The XHTML (Form)
<p><strong>URL:</strong> <input type="text" id="url" size="40" /> <input type="button" id="geturl" value="Get URL" /></p>
<p id="newurl"></p>
We need an input box where the user will enter their a URL, a button to trigger the process, and a placeholder to put the new, tiny URL.
TinyURL is an awesome service. For those who don’t know what TinyURL is, TinyURL allows you to take a long URL like “http://davidwalsh.name/jquery-link-nudging” and turn it into “http://tinyurl.com/67c4se”. Using the PHP and TinyURL API, you can create these tiny URLs on the fly!
The PHP
//gets the data from a URL
function get_tiny_url($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,'http://tinyurl.com/api-create.php?url='.$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
//test it out!
$new_url = get_tiny_url('http://davidwalsh.name/php-imdb-information-grabber');
//returns http://tinyurl.com/65gqpp
echo $new_url
Simply provide the URL and you’ll received the new, tiny URL in return. If you use Twitter, you’ll have noticed that Twitter automates tiny URL creation for URLs in tweets.
It’s usually best to repair broken image paths as soon as possible because they can damage a website’s credibility. And even worse is having a user tell you about it. Using jQuery and PHP, you can have your page automatically notify you of broken images.
The PHP
if(isset($_POST['image']))
{
$to = 'errors@yourdomain.com';
$from = 'automailer@yourdomain.com';
$subject = 'Broken Image';
$content = "The website is signaling a broken image!\n\nBroken Image Path: ".stripslashes($_POST['image'])."\n\nReferenced on Page: ".stripslashes($_POST['page']);
$result = mail($to,$subject,$content,'From: '.$from."\r\n");
die($result);
}
I keep the email short and to the point; it contains the broken image’s “src” attribute and the page it was requested by.