Login

Building a Form to Count Back Links Using Yahoo Inbound Links API

In the first part of this three-part series, you learned the guiding concepts and design for building a PHP web application to count back links from unique domains using the Yahoo Site Explorer API. In this part, we will illustrate, in detail, how to do the programming for the PHP Ajax web form and the validation script.

At the end of the third part, I’ll give you a link which you can use to download all the files associated with this web application project.

Passing form variables to PHP via AJAX

The script uses captcha code that will pass the answer for the challenge using a PHP session, so at the top of the web form (file name: countuniquelinks.php), this code is placed:

<?php

session_start();

?>

Discussion of captcha generation is out of the scope of this article. However, you can read another article on this site for a detailed discussion of how to build a captcha system.

Shown below this HTML web form screen shot is the source code required to produce the form.

<h3>This tool will count the number of back links coming from unique domains.</h3>

domainurl = (Using text field) This is the user domain URL input to the form.

Options = (Using radio buttons) This covers the result options. The value is 1 if the user is interested in getting the results for the backlinks pointing to the entire site, not just to one specific URL. Otherwise, it is assigned 2.

Captcha = (Using text field) This is the user captcha input value.

Submitting values to PHP via AJAX means that the page does not need to be reloaded or refreshed. The JavaScript code that will communicate to PHP is the following, which is placed above the HTML web form discussed earlier (at the <head> tag, where most JavaScripts are located).

Using the AJAX Prototype method, the form variables are passed as parameters as shown above. PHP provides the results via the showResponse function, which will be shown on the same page as the web form (and below it):

Source code:

<p id="show"></p>

</body>

</html>

The id=”show” is an indicator that AJAX will look to send the results back as provided by PHP, according to this query:

function showResponse(req){

$(‘show’).innerHTML= req.responseText;

}

Other things to note about the AJAX query provided above is the path to the PHP script:

new Ajax.Request("/countuniquelinks/yahooapiajax.php",

When the submit button is pressed, without reloading the page, AJAX will send variables to the PHP script “yahooapiajax.php” in the background, which will process the variables, validate them and then query Yahoo’s inlinks API.

echo ‘ERROR: You have not entered captcha code or it is entered incorrectly.’;

die ();

}

{mospagebreak title=Validating the Root Domain Name URL using PHP}

It is highly important to validate user input on the root domain URL after they have entered it on the web form. If this is not validated, or not in the proper URL syntax as required by the Yahoo Site Explorer API, the API won’t be able to process the request, or it will return inaccurate data.

The API currently accepts URLs that start with a protocol (http:// or https://) and do not end with a trailing slash. Of course the URL should also be in the correct format (domain name + ‘.’ + TLD). Examples of TLDs (top level domain) are .com, .gov, .net, .org.

The first thing to be checked is that the domain URL field is not empty:

if (empty($domainurl)) {

//name field is blank

echo ‘ERROR: The homepage URL field is empty.’;

die ();

}

If the URL is not empty, we will need to validate that the user correctly entered the protocol (http:// or https://) as well as whether or not there are trailing slashes on the end:

$httpprotocol= substr($domainurl, 0, 7);

$httpprotocolwrong=substr($domainurl, 0, 8);

$httpsprotocol =substr($domainurl, 0, 8);

$httpsprotocolwrong= substr($domainurl, 0, 9);

$trailingslash = substr($domainurl, -1);

$firstdot= strpos($domainurl, ‘.’);

$offset=$firstdot + 1;

$seconddot= strpos($domainurl,’.’,$offset);

The substr function will extract the first seven characters if the protocol of the root domain is http:// or extract the first eight characters if the protocol is https://.

Also, you need to check other errors commonly made by the user when inputting domain URLs, such as http:/// or https:///.

There should only be two slashes in the protocol. These are defined via:

$httpprotocolwrong=substr($domainurl, 0, 8);

$httpsprotocolwrong= substr($domainurl, 0, 9);

The presence of a trailing slash at the end of the URL is defined by:

$trailingslash = substr($domainurl, -1);

If there are no dots in the URL, then it is considered a malformed URL, which is defined by these PHP variables:

$firstdot= strpos($domainurl, ‘.’);

$offset=$firstdot + 1;

$seconddot= strpos($domainurl,’.’,$offset);

The actual validation script for detecting allowable and correct protocols:

if (!(($httpprotocol==’http://’) || ($httpsprotocol==’https://’))) {

echo ‘ERROR: Root domain URL protocol should either only be http:// or https://’;

die ();

}

This means that, if the user input domain URL does not contain either http:// or https://, then an error will be displayed to them so they can correct the entry.

Detecting malformed protocol such as https:/// or http:/// follows a similar concept of validation:

echo ‘ERROR: Root domain URL protocol should either only be http:// or https://’;

die ();

}

If the user inputs a trailing slash at the end of the URL, then an error will also display:

if ($trailingslash==’/’) {

echo ‘ERROR: Do not include trailing slash "/" at the end of the URL’;

die ();

}

Please note that all of the validating variables used in the above validation script are first defined earlier.

To determine whether or not the domain URL is malformed (for example: http://wwwphpdeveloperorg or http://www.phpdeveloperorg), the validation line will be:

if (($firstdot==”) && ($seconddot==”)) {

echo ‘ERROR: Malformed domain root URL’;

die ();

}

Finally, one of the additional user input items is the options (whether to get backlinks data pointing to the entire site or just to a specific URL):

if ($options1==1) {

$options=1;

}

elseif ($options2==2) {

$options=”;

}

else {

echo ‘ERROR: The options field is empty.’;

die ();

}

According to the Yahoo Site Explorer API guide, if the entire_site parameter is set to “1,” then it will return backlinks pointing to the entire site; otherwise, if left empty “”, then it will retrieve back links pointing to that specific URL only. These definitions have been included above. So if the $options1 variable value from the form is 1, assign 1 to the final $options variable (to be passed to the Yahoo inlinks API); otherwise, if it is 2, then assign it to be empty “”."

In the third part, you will learn the PHP script pertaining to the details of API communication and data analysis.