php使用socket调用http和smtp协议实例小结
程序员文章站
2022-06-14 14:28:09
本文实例讲述了php使用socket调用http和smtp协议。分享给大家供大家参考,具体如下:
socket发送http请求
http协议请求报文格式
get...
本文实例讲述了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);
参考文章: