PHP Cookbook读书笔记 – 第16章互联网服务
發(fā)送電子郵件
書(shū)中主要是以PEAR中的郵件發(fā)送類(lèi)(Mail)來(lái)講解的(關(guān)于如何在WIN系統(tǒng)下安裝PEAR可以參考WIN下成功安裝PEAR)。PEAR的MAIL類(lèi)可以通過(guò)3種方式來(lái)發(fā)送電子郵件:
書(shū)中主要以第一種方式來(lái)講解,下面貼一個(gè)用SMTP發(fā)送郵件的例子
require_once('Mail.php');//包含mail.php函數(shù)$params = array('host' => 'smtp.sina.com','port' => '25','username' => 'yourname@sina.com','password' => 'yourpassword','auth' => true);//必須保證這一行$recipients = 'xxxx@sina.com'; //接收人,可以是一個(gè)數(shù)組來(lái)存放多個(gè)地址$headers['From'] = "yourname@sina.com";$headers['To'] = "xxxx@sina.com";$headers['Subject'] = "主題";$body = "內(nèi)容";//選擇smtp的發(fā)送方式,當(dāng)然還支持mail()和sendmail$mail_object = &Mail::factory('smtp', $params);if (PEAR::isError($e = $mail_object->send($recipients, $headers, $body))) {die($e->getMessage() . "\n");}通過(guò)IMAP或POP3讀取郵件
這種方式主要用來(lái)讀取郵件的內(nèi)容,像開(kāi)心網(wǎng)等都有個(gè)導(dǎo)入郵箱聯(lián)系人的功能,我原來(lái)一直以為是通過(guò)這2個(gè)協(xié)議來(lái)實(shí)現(xiàn)的,后來(lái)查了下資料才發(fā)現(xiàn),原來(lái)是通過(guò)cUrl實(shí)現(xiàn)的。
// 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訪(fǎng)問(wèn)FTP資源
通常有兩種方式:
下面是通過(guò)PHP內(nèi)置函數(shù)實(shí)現(xiàn)的讀取FTP的目錄下的文件列表
set_time_limit(120); $host="127.0.0.1";//主機(jī)名 $uname="username";//用戶(hù)名 $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相關(guān)函數(shù)
查詢(xún)一個(gè)域名的ip地址:gethostbyname( )
查詢(xún)一個(gè)IP上的域名(不完全可信):gethostbyaddr( )
一個(gè)域名可能綁定多個(gè)IP:gethostbynamel( )
獲得mx記錄:getmxrr( )
實(shí)現(xiàn)PING一個(gè)主機(jī)
PEAR提供的Net_Ping包可以實(shí)現(xiàn)這個(gè)功能
$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"; }返回的信息是一個(gè)整塊,需要其中的個(gè)別值還需要自己解析,實(shí)現(xià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獲取域名相關(guān)的信息
需要用到PEAR的另一個(gè)類(lèi)Net_Whois,其中whois服務(wù)器的選擇是這個(gè)功能的關(guān)鍵,如果不知道如何選擇whois服務(wù)器,可以將$server用'whois.internic.net'替換:
require 'Net/Whois.php'; $server = 'whois.networksolutions.com'; $query = 'example.org'; $data = Net_Whois::query($server, $query);轉(zhuǎn)載于:https://www.cnblogs.com/Excellent/archive/2011/11/21/2257227.html
總結(jié)
以上是生活随笔為你收集整理的PHP Cookbook读书笔记 – 第16章互联网服务的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 母婴店加盟需要多少钱啊?
- 下一篇: PHP文件系统-文件下载