本篇文章給大家分享的是有關(guān)怎么在php中使用socket調(diào)用http和smtp協(xié)議,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
創(chuàng)新互聯(lián)公司"三網(wǎng)合一"的企業(yè)建站思路。企業(yè)可建設(shè)擁有電腦版、微信版、手機(jī)版的企業(yè)網(wǎng)站。實(shí)現(xiàn)跨屏營(yíng)銷,產(chǎn)品發(fā)布一步更新,電腦網(wǎng)絡(luò)+移動(dòng)網(wǎng)絡(luò)一網(wǎng)打盡,滿足企業(yè)的營(yíng)銷需求!創(chuàng)新互聯(lián)公司具備承接各種類型的做網(wǎng)站、成都網(wǎng)站建設(shè)項(xiàng)目的能力。經(jīng)過(guò)十載的努力的開拓,為不同行業(yè)的企事業(yè)單位提供了優(yōu)質(zhì)的服務(wù),并獲得了客戶的一致好評(píng)。socket發(fā)送HTTP請(qǐng)求
http協(xié)議請(qǐng)求報(bào)文格式
get
## 請(qǐng)求方法 請(qǐng)求文件路徑?查詢字符串 HTTP/協(xié)議版本號(hào) ## Host: 主機(jī)名:端口號(hào) ## Connection:close
post
## 請(qǐng)求方法 請(qǐng)求文件路徑 HTTP/協(xié)議版本號(hào) ## Host: 主機(jī)名:端口號(hào) ## Content-type: application/x-www-form-urlencoded ## Content-length: 查詢字符串長(zhǎng)度 ## Connection: close ## 查詢字符串
api.php
"; print_r($_REQUEST);
fsocketopen發(fā)送get請(qǐng)求
"zhezhao","age"=>23]; $url .= "?".http_build_query($param); $socket = fsockopen($host,$port,$errno,$errstr,10); $request = "GET /${url} HTTP/1.1\r\n"; $request .= "Host:${host}:{$port}\r\n"; $request .= "Connection:close\r\n\r\n"; fwrite($socket,$request); echo "Request:
".str_replace("\r\n","
",$request); echo "
Response:
"; $response = ""; while ($out = fread($socket, 2048)) { $response .= $out; } echo str_replace("\r\n","
",$response); fclose($socket);
sockets擴(kuò)展發(fā)送post請(qǐng)求
"zhezhao","age"=>23]; $data = http_build_query($param); $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_connect($socket, $host,$port); $request = "POST /${url} HTTP/1.1\r\n"; $request .= "Host:${host}:{$port}\r\n"; $request .= "Content-type:application/x-www-form-urlencoded\r\n"; $request .= "Content-length:".strlen($data)."\r\n"; $request .= "Connection:close\r\n\r\n"; $request .= "${data}"; socket_write($socket,$request); echo "Request:
".str_replace("\r\n","
",$request); echo "
Response:
"; $response = ""; while ($out = socket_read($socket, 2048)) { $response .= $out; } echo str_replace("\r\n","
",$response); socket_close($socket);
socket發(fā)送smtp請(qǐng)求
首先通過(guò)socket連接到smtp服務(wù)器,例如smtp.163.com 25,然后實(shí)現(xiàn)smtp發(fā)送郵件的必須參數(shù)即可。
HELO 主機(jī)名
AUTH LOGIN base64編碼之后的發(fā)件人郵箱
base64編碼之后的密碼
##填寫信封
MAIL FROM:<發(fā)件人郵箱>
RCPT TO:<收件人郵箱>
##填寫信件內(nèi)容
DATA
MIME-Version:1.0
To: 收件人郵箱
From: 發(fā)件人郵箱
Subject: 郵件主題郵件內(nèi)容
fsocketopen發(fā)送郵件
fsocketopen
和pfsocketopen
是php機(jī)制本身的函數(shù),可以直接調(diào)用,不需要安裝擴(kuò)展。以上兩個(gè)函數(shù)可以打開一個(gè)網(wǎng)絡(luò)連接或者一個(gè)Unix套接字連接,然后我們可以使用fwrite
,fread
,fclose
,feof等函數(shù)直接操作該網(wǎng)絡(luò)連接。
"; fputs($sock,"AUTH LOGIN ".base64_encode($user)."\r\n"); echo fgets($sock, 512)."
"; fputs($sock,base64_encode($password)."\r\n"); echo fgets($sock, 512)."
"; fputs($sock,"MAIL FROM:<${user}>\r\n"); echo fgets($sock, 512)."
"; fputs($sock,"RCPT TO:<${mailto}>\r\n"); echo fgets($sock, 512)."
"; fputs($sock,"DATA\r\n"); echo fgets($sock, 512)."
"; $header = "MIME-Version:1.0\r\n"; $header .= "To: ${mailto}\r\n"; $header .= "From: ${user}\r\n"; $header .= "Subject: ${subject}\r\n"; fputs($sock, $header . "\r\n" . $body); echo fgets($sock, 512)."
"; fputs($sock, "\r\n.\r\n"); echo fgets($sock, 512)."
"; fputs($sock,"QUIT\r\n"); fclose($sock);
sockets擴(kuò)展發(fā)送郵件
sockets擴(kuò)展的功能更加強(qiáng)大,可以搭建tcp/ip服務(wù)器,不過(guò)我們使用smtp協(xié)議發(fā)送郵件的例子,只需要使用到socket客戶端的功能。
"; socket_write($socket,"AUTH LOGIN ".base64_encode($user)."\r\n"); echo socket_read($socket, 512)."
"; socket_write($socket,base64_encode($password)."\r\n"); echo socket_read($socket, 512)."
"; socket_write($socket,"MAIL FROM:<${user}>\r\n"); echo socket_read($socket, 512)."
"; socket_write($socket,"RCPT TO:<${mailto}>\r\n"); echo socket_read($socket, 512)."
"; socket_write($socket,"DATA\r\n"); echo socket_read($socket, 512)."
"; $header = "MIME-Version:1.0\r\n"; $header .= "To: ${mailto}\r\n"; $header .= "From: ${user}\r\n"; $header .= "Subject: ${subject}\r\n"; socket_write($socket, $header . "\r\n" . $body); echo socket_read($socket, 512)."
"; socket_write($socket, "\r\n.\r\n"); echo socket_read($socket, 512)."
"; socket_write($socket,"QUIT\r\n"); fclose($sock);
以上就是怎么在php中使用socket調(diào)用http和smtp協(xié)議,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。