php使用socket调用http和smtp协议实例小结
发布:smiling 来源: PHP粉丝网 添加日期:2021-12-07 20:20:08 浏览: 评论:0
这篇文章主要介绍了php使用socket调用http和smtp协议,结合实例形式总结分析了php使用socket发送http请求、post请求、SMTP请求、邮件发送等相关操作技巧,需要的朋友可以参考下。
本文实例讲述了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);
Tags: socket http smtp
相关文章
- ·php socket讲解与实例(2013-12-09)
- ·PHP socket模拟POST请求实例(2014-01-18)
- ·php socket 使用smtp服务器发送邮件(2014-01-22)
- ·php中socket实现GET与POST异步提交数据(2014-06-16)
- ·php socket客户端和服务端互相通讯实例(2014-06-19)
- ·PHP中向socket服务器收发数据(2014-08-28)
- ·PHP利用socket模拟post之fsockopen发送数据(2014-09-05)
- ·php Socket 创建 监听等实例(2014-09-09)
- ·php socket通信机制实例说明与代码(2014-09-09)
- ·PHP不支持socket_connect函数开启步骤(2014-09-10)
- ·php基于Socket实现多线程开发教程(2015-04-15)
- ·PHP聊天室_WebSocket技术实战(2016-08-25)
- ·php中socket服务的模型下的编程方式(同步和异步)(2018-09-18)
- ·php socket服务的模型以及实现 多进程IO复用libevent(2018-09-18)
- ·php实现socket推送技术的示例(2018-11-04)
- ·php与flash as3 socket通信传送文件实现代码(2021-03-31)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)