您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關怎么在php中使用socket調用http和smtp協議,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
socket發送HTTP請求
http協議請求報文格式
get
## 請求方法 請求文件路徑?查詢字符串 HTTP/協議版本號 ## Host: 主機名:端口號 ## Connection:close
post
## 請求方法 請求文件路徑 HTTP/協議版本號 ## Host: 主機名:端口號 ## Content-type: application/x-www-form-urlencoded ## Content-length: 查詢字符串長度 ## Connection: close ## 查詢字符串
api.php
<?php echo $_SERVER["REQUEST_METHOD"]."<br/>"; print_r($_REQUEST);
fsocketopen發送get請求
<?php $url = "api.php"; $host = "127.0.0.1"; $port = 8080; $param = ["name"=>"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 "<b>Request:</b><br/>".str_replace("\r\n","<br/>",$request); echo "<hr/><b>Response:</b><br/>"; $response = ""; while ($out = fread($socket, 2048)) { $response .= $out; } echo str_replace("\r\n","<br/>",$response); fclose($socket);
sockets擴展發送post請求
<?php $url = "api.php"; $host = "127.0.0.1"; $port = 8080; $param = ["name"=>"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 "<b>Request:</b><br/>".str_replace("\r\n","<br/>",$request); echo "<hr/><b>Response:</b><br/>"; $response = ""; while ($out = socket_read($socket, 2048)) { $response .= $out; } echo str_replace("\r\n","<br/>",$response); socket_close($socket);
socket發送smtp請求
首先通過socket連接到smtp服務器,例如smtp.163.com 25,然后實現smtp發送郵件的必須參數即可。
HELO 主機名
AUTH LOGIN base64編碼之后的發件人郵箱
base64編碼之后的密碼
##填寫信封
MAIL FROM:<發件人郵箱>
RCPT TO:<收件人郵箱>
##填寫信件內容
DATA
MIME-Version:1.0
To: 收件人郵箱
From: 發件人郵箱
Subject: 郵件主題郵件內容
fsocketopen發送郵件
fsocketopen
和pfsocketopen
是php機制本身的函數,可以直接調用,不需要安裝擴展。以上兩個函數可以打開一個網絡連接或者一個Unix套接字連接,然后我們可以使用fwrite
,fread
,fclose
,feof等函數直接操作該網絡連接。
<?php $user = "root@163.com"; $password = "root"; $mailto = "1234567@qq.com"; $subject = "fsocketopen發送郵件"; $body = "收到郵件了么,親"; $sock = fsockopen("smtp.163.com",25); fputs($sock,"HELO localhost\r\n"); echo fgets($sock, 512)."<br/>"; fputs($sock,"AUTH LOGIN ".base64_encode($user)."\r\n"); echo fgets($sock, 512)."<br/>"; fputs($sock,base64_encode($password)."\r\n"); echo fgets($sock, 512)."<br/>"; fputs($sock,"MAIL FROM:<${user}>\r\n"); echo fgets($sock, 512)."<br/>"; fputs($sock,"RCPT TO:<${mailto}>\r\n"); echo fgets($sock, 512)."<br/>"; fputs($sock,"DATA\r\n"); echo fgets($sock, 512)."<br/>"; $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)."<br/>"; fputs($sock, "\r\n.\r\n"); echo fgets($sock, 512)."<br/>"; fputs($sock,"QUIT\r\n"); fclose($sock);
sockets擴展發送郵件
sockets擴展的功能更加強大,可以搭建tcp/ip服務器,不過我們使用smtp協議發送郵件的例子,只需要使用到socket客戶端的功能。
<?php $user = "root@163.com"; $password = "root"; $mailto = "1234567@qq.com"; $subject = "sockets擴展發送郵件"; $body = "收到郵件了么,親"; $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_connect($socket, "smtp.163.com",25); socket_write($socket,"HELO localhost\r\n"); echo socket_read($socket, 512)."<br/>"; socket_write($socket,"AUTH LOGIN ".base64_encode($user)."\r\n"); echo socket_read($socket, 512)."<br/>"; socket_write($socket,base64_encode($password)."\r\n"); echo socket_read($socket, 512)."<br/>"; socket_write($socket,"MAIL FROM:<${user}>\r\n"); echo socket_read($socket, 512)."<br/>"; socket_write($socket,"RCPT TO:<${mailto}>\r\n"); echo socket_read($socket, 512)."<br/>"; socket_write($socket,"DATA\r\n"); echo socket_read($socket, 512)."<br/>"; $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)."<br/>"; socket_write($socket, "\r\n.\r\n"); echo socket_read($socket, 512)."<br/>"; socket_write($socket,"QUIT\r\n"); fclose($sock);
以上就是怎么在php中使用socket調用http和smtp協議,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。