note 49290 added to function.getmxrr
| From: | rune dot heggtveit at devzone dot progative dot com | Date: | Sun, 23 Jan 2005 09:02:10 +0000 |
| Subject: | note 49290 added to function.getmxrr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-83802@lists.php.net to get a copy of this message | ||
An other way to do mx-lookup on a windows platform.
Rewrote this from an other class i wrote for DNS lookup - so it might be a bit messy - but hope you
get the idea.
Big thanks to the rfc community.
<?php
class mxlookup
{
var $dns_socket = NULL;
var $QNAME = "";
var $dns_packet= NULL;
var $ANCOUNT = 0;
var $cIx = 0;
var $dns_repl_domain;
var $arrMX = array();
function mxlookup($domain, $dns="192.168.2.1")
{
$this->QNAME($domain);
$this->pack_dns_packet();
$dns_socket = fsockopen("udp://$dns", 53);
fwrite($dns_socket,$this->dns_packet,strlen($this->dns_packet));
$this->dns_reply = fread($dns_socket,1);
$bytes = stream_get_meta_data($dns_socket);
$this->dns_reply .= fread($dns_socket,$bytes['unread_bytes']);
fclose($dns_socket);
$this->cIx=6;
$this->ANCOUNT = $this->gord(2);
$this->cIx+=4;
$this->parse_data($this->dns_repl_domain);
$this->cIx+=7;
for($ic=1;$ic<=$this->ANCOUNT;$ic++)
{
$QTYPE = ord($this->gdi($this->cIx));
if($QTYPE!==15){print("[MX Record not returned]"); die();}
$this->cIx+=9;
$mxPref = ord($this->gdi($this->cIx));
$this->parse_data($curmx);
$this->arrMX[] = array("MX_Pref" => $mxPref, "MX" =>
$curmx);
$this->cIx+=3;
}
}
function parse_data(&$retval)
{
$arName = array();
$byte = ord($this->gdi($this->cIx));
while($byte!==0)
{
if($byte==192) //compressed
{
$tmpIx = $this->cIx;
$this->cIx = ord($this->gdi($cIx));
$tmpName = $retval;
$this->parse_data($tmpName);
$retval=$retval.".".$tmpName;
$this->cIx = $tmpIx+1;
return;
}
$retval="";
$bCount = $byte;
for($b=0;$b<$bCount;$b++)
{
$retval .= $this->gdi($this->cIx);
}
$arName[]=$retval;
$byte = ord($this->gdi($this->cIx));
}
$retval=join(".",$arName);
}
function gdi(&$cIx,$bytes=1)
{
$this->cIx++;
return(substr($this->dns_reply, $this->cIx-1, $bytes));
}
function QNAME($domain)
{
$dot_pos = 0; $temp = "";
while($dot_pos=strpos($domain,"."))
{
$temp = substr($domain,0,$dot_pos);
$domain = substr($domain,$dot_pos+1);
$this->QNAME .= chr(strlen($temp)).$temp;
}
$this->QNAME .= chr(strlen($domain)).$domain.chr(0);
}
function gord($ln=1)
{
$reply="";
for($i=0;$i<$ln;$i++){
$reply.=ord(substr($this->dns_reply,$this->cIx,1));
$this->cIx++;
}
return $reply;
}
function pack_dns_packet()
{
$this->dns_packet = chr(0).chr(1).
chr(1).chr(0).
chr(0).chr(1).
chr(0).chr(0).
chr(0).chr(0).
chr(0).chr(0).
$this->QNAME.
chr(0).chr(15).
chr(0).chr(1);
}
}
?>
<?php
/* Exampe of use: */
$mx = new mxlookup("php.net");
print $mx->ANCOUNT." MX Records\n";
print "Records returned for ".$mx->dns_repl_domain.":\n<pre>";
print_r($mx->arrMX);
?>
Return:
02 MX Records Records returned for php.net:
Array
(
[0] => Array
(
[MX_Pref] => 15
[MX] => smtp.osuosl.org
)
[1] => Array
(
[MX_Pref] => 5
[MX] => osu1.php.net
)
)
----
Manual Page -- http://www.php.net/manual/en/function.getmxrr.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49290
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49290&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49290&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49290&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49290&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49290&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49290&report=yes
Search -- http://master.php.net/manage/user-notes.php