PHP PEAR on Windows and Plesk 9.5

While writing an application on my linux machine , i used the PHP function (getmxrr) to resolve MX records, the lovely function returns an array of all the MX records that are associated with a domain name, once i moved the application to a windows machine that ran PHP 5.2.13 on Plesk 9.5 , things went bam, getmxrr is only available on Windows when you are running PHP 5.3 and above, so what to do

The answer was the PEAR package (Net_DNS), here i will document how i installed and used it so that i don’t have to look around again later

Here are the exact steps i had to follow to get things working, including the code :)

1- Login to the windows server via remote desktop

2- Open the command line

3- on the command line

cd C:\Parallels\Plesk\Additional\Php

Once in that directory, you can execute (on the command line)

go-pear

You should now follow the instructions, install the extra package, and you have pear installed.

Now, a reg file should be in that same directory, double click it, enter it into the registry

Now you need to edit php.ini found at C:\Parallels\Plesk\Additional\PleskPHP5

in the path variable, add the pear folder

include_path = ".;./includes;./pear;C:\Parallels\Plesk\Additional\Php\PEAR"

Now, i restarted the server, you can restart IIS or do that IIS magic you know how to do (No, it is not /etc/init.d/IIS restart), i am just too lazy, it is probably very easy

Now, on the command line, i executed

pear install Net_DNS-1.0.7

i had to do it twice, and for some reason the first run did not find the package, second installed, very strange i think.

Now that the thing was installed, i simply ran the following code, and i got a result very similar to that of my beloved getmxrr

<?php

function extract_answers_mx($answerobj)
{
	//this one extracts the MX records !
	$innerobjary = $answerobj->answer;
	$numofrecs = count($innerobjary);//How many records ?
	$retary = array();//we store servers here...
	if((is_array($innerobjary)) && (count($innerobjary) > 0))
	{
		foreach ($innerobjary as $thisent)
		{
			//we have an entry from the array !
			$retary[] = $thisent->exchange;//put the exchange in the array
		}
	}
	return $retary;//could be an empty array !
}

require_once 'Net/DNS.php';
$resolver = new Net_DNS_Resolver();
$resolver->debug = 0; // Turn on debugging output to show the query
$resolver->usevc = 1; // Force the use of TCP instead of UDP
$resolver->nameservers = array(              // Set the IP addresses
                           '192.168.2.100',     // of the nameservers
                           '192.168.2.101'  // to query.
                           );
$response = $resolver->query('php.net', 'MX');
if (! $response)
{
  echo "\n";
  echo "ANCOUNT is 0, therefore the query() 'failed'\n";
  echo "See Net_DNS_Resolver::rawQuery() to receive this packet\n";
}
$retary = extract_answers_mx($response);
foreach ($retary as $thisary)
{
	print "{$thisary}\n";
}
?>

And now, i can go back to doing my magic :)

Leave a Reply