發送電子郵件
書中主要是以PEAR中的郵件發送類(Mail)來講解的(關于如何在WIN系統下安裝PEAR可以參考WIN下成功安裝PEAR)。PEAR的MAIL類可以通過3種方式來發送電子郵件:
- 通過PHP內部的mail函數來發送
- 通過sendmail程序來發送
- 通過直接連接到一個smtp服務器發送郵件
書中主要以第一種方式來講解,下面貼一個用SMTP發送郵件的例子
require_once('Mail.php');//包含mail.php函數$params = array('host' => 'smtp.sina.com','port' => '25','username' => 'yourname@sina.com','password' => 'yourpassword','auth' => true);//必須保證這一行$recipients = 'xxxx@sina.com'; //接收人,可以是一個數組來存放多個地址$headers['From'] = "yourname@sina.com";$headers['To'] = "xxxx@sina.com";$headers['Subject'] = "主題";$body = "內容";//選擇smtp的發送方式,當然還支持mail()和sendmail$mail_object = &Mail::factory('smtp', $params);if (PEAR::isError($e = $mail_object->send($recipients, $headers, $body))) {die($e->getMessage() . "\n");}
通過IMAP或POP3讀取郵件
這種方式主要用來讀取郵件的內容,像開心網等都有個導入郵箱聯系人的功能,我原來一直以為是通過這2個協議來實現的,后來查了下資料才發現,原來是通過cUrl實現的。
// open IMAP connection
$mail = imap_open('{mail.server.com:143}', 'username', 'password');
// or, open POP3 connection
$mail = imap_open('{mail.server.com:110/pop3}', 'username', 'password');// grab a list of all the mail headers
$headers = imap_headers($mail);// grab a header object for the last message in the mailbox
$last = imap_num_msg($mail);
$header = imap_header($mail, $last);// grab the body for the same message
$body = imap_body($mail, $last);// close the connection
imap_close($mail);
PHP訪問FTP資源
通常有兩種方式:
- PHP內置的FTP函數
- cURL擴展
下面是通過PHP內置函數實現的讀取FTP的目錄下的文件列表
set_time_limit(120);
$host="127.0.0.1";//主機名
$uname="username";//用戶名
$pwd ="password";//密碼
$c = ftp_connect($host) or die("Can't connect");
ftp_set_option($c,FTP_TIMEOUT_SEC,120);
ftp_login($c,$uname,$pwd) or die("Can't login");
ftp_pasv($c, TRUE);
$ddd = ftp_nlist($c,".");
ftp_close($c) or die("Can't close");
var_dump($ddd);
print("OK");
DNS相關函數
查詢一個域名的ip地址:gethostbyname( )
查詢一個IP上的域名(不完全可信):gethostbyaddr( )
一個域名可能綁定多個IP:gethostbynamel( )
獲得mx記錄:getmxrr( )
實現PING一個主機
PEAR提供的Net_Ping包可以實現這個功能
$results = $ping->ping('www.oreilly.com');
foreach($results as $result) { print "$result\n"; }
$results = $ping->ping('www.oreilly.com');
foreach($results as $result) { print "$result\n"; }
返回的信息是一個整塊,需要其中的個別值還需要自己解析,實現的效果可能如下面顯示的這樣
PING www.oreilly.com (209.204.146.22) from 192.168.123.101 :32(60) bytes of data. 40 bytes from www.oreilly.com (209.204.146.22): icmp_seq=0 ttl=239time=96.704 msec 40 bytes from www.oreilly.com (209.204.146.22): icmp_seq=1 ttl=239time=86.567 msec 40 bytes from www.oreilly.com (209.204.146.22): icmp_seq=2 ttl=239time=86.563 msec 40 bytes from www.oreilly.com (209.204.146.22): icmp_seq=3 ttl=239time=136.565 msec 40 bytes from www.oreilly.com (209.204.146.22): icmp_seq=4 ttl=239time=86.627 msec-- - www.oreilly.com ping statistics -- - 5 packets transmitted, 5 packets received, 0% packet loss round-trip min/avg/max/mdev = 86.563/98.605/136.565/19.381 ms
獲取域名相關的信息
需要用到PEAR的另一個類Net_Whois,其中whois服務器的選擇是這個功能的關鍵,如果不知道如何選擇whois服務器,可以將$server用'whois.internic.net'替換:
require 'Net/Whois.php';
$server = 'whois.networksolutions.com';
$query = 'example.org';
$data = Net_Whois::query($server, $query);