十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本篇文章给大家分享的是有关怎么在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
"; print_r($_REQUEST);
fsocketopen发送get请求
"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扩展发送post请求
"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发送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等函数直接操作该网络连接。
"; 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扩展发送邮件
sockets扩展的功能更加强大,可以搭建tcp/ip服务器,不过我们使用smtp协议发送邮件的例子,只需要使用到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调用http和smtp协议,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。