本文实例讲述了php使用socket调用http和smtp协议。分享给大家供大家参考,具体如下:
socket发送HTTP请求
http协议请求报文格式
get
## 请求方法 请求文件路径"htmlcode">## 请求方法 请求文件路径 HTTP/协议版本号 ## Host: 主机名:端口号 ## Content-type: application/x-www-form-urlencoded ## Content-length: 查询字符串长度 ## Connection: close ## 查询字符串api.php
<"REQUEST_METHOD"]."<br/>"; print_r($_REQUEST);fsocketopen发送get请求
<"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请求
<"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等函数直接操作该网络连接。<"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客户端的功能。
<"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);参考文章:
https://www.jb51.net/article/66594.htm
https://www.jb51.net/article/114394.htm
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php socket用法总结》、《php字符串(string)用法总结》、《PHP数学运算技巧总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》及《PHP网络编程技巧总结》
希望本文所述对大家PHP程序设计有所帮助。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- [ABC]蔡琴《百万琴歌[6N纯银镀膜]》[低速原抓WAV+CUE]
- 群星《国风超有戏 第7期》[FLAC/分轨][147.99MB]
- 群星《闪光的夏天 第3期》[320K/MP3][61.94MB]
- 群星《闪光的夏天 第3期》[FLAC/分轨][336MB]
- 【迷幻电音】Elea-2024-Hypnos(FLAC)
- 【民族融合】VA-2024-TheOrientCollective:GoldenSand(FLAC)
- 谭嘉仪-EyesOnMe新曲+精选2022【低速原抓WAV+CUE】
- 尚士达《莫回头》[320K/MP3][184.64MB]
- 尚士达《莫回头》[Hi-Res][24bit 48kHz][FLAC/分轨][1.27G]
- 群星《奔赴!万人现场 第3期》[320K/MP3][98.9MB]
- 谭嘉仪《Lonely》【WAV+CUE】
- 群星《红色钢琴浪漫曲》2CD【WAV+CUE】
- 凤飞飞《浮世情怀》HQCD[正版原抓WAV+CUE]
- 群星《奔赴!万人现场 第3期》[FLAC/分轨][537.75MB]
- 群星 《2024好听新歌23》十倍音质 U盘音乐 [WAV分轨][1.6G]