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

<?phpecho $_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.1rn";$request .= "Host:${host}:{$port}rn";$request .= "Connection:closernrn";fwrite($socket,$request);echo "<b>Request:</b><br/>".str_replace("rn","<br/>",$request);echo "<hr/><b>Response:</b><br/>";$response = "";while ($out = fread($socket, 2048)) { $response .= $out;}echo str_replace("rn","<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.1rn";$request .= "Host:${host}:{$port}rn";$request .= "Content-type:application/x-www-form-urlencodedrn";$request .= "Content-length:".strlen($data)."rn";$request .= "Connection:closernrn";$request .= "${data}";socket_write($socket,$request);echo "<b>Request:</b><br/>".str_replace("rn","<br/>",$request);echo "<hr/><b>Response:</b><br/>";$response = "";while ($out = socket_read($socket, 2048)) { $response .= $out;}echo str_replace("rn","<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发送邮件

fsocketopenpfsocketopen是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 localhostrn");echo fgets($sock, 512)."<br/>";fputs($sock,"AUTH LOGIN ".base64_encode($user)."rn");echo fgets($sock, 512)."<br/>";fputs($sock,base64_encode($password)."rn");echo fgets($sock, 512)."<br/>";fputs($sock,"MAIL FROM:<${user}>rn");echo fgets($sock, 512)."<br/>";fputs($sock,"RCPT TO:<${mailto}>rn");echo fgets($sock, 512)."<br/>";fputs($sock,"DATArn");echo fgets($sock, 512)."<br/>";$header = "MIME-Version:1.0rn";$header .= "To: ${mailto}rn";$header .= "From: ${user}rn";$header .= "Subject: ${subject}rn";fputs($sock, $header . "rn" . $body);echo fgets($sock, 512)."<br/>";fputs($sock, "rn.rn");echo fgets($sock, 512)."<br/>";fputs($sock,"QUITrn");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 localhostrn");echo socket_read($socket, 512)."<br/>";socket_write($socket,"AUTH LOGIN ".base64_encode($user)."rn");echo socket_read($socket, 512)."<br/>";socket_write($socket,base64_encode($password)."rn");echo socket_read($socket, 512)."<br/>";socket_write($socket,"MAIL FROM:<${user}>rn");echo socket_read($socket, 512)."<br/>";socket_write($socket,"RCPT TO:<${mailto}>rn");echo socket_read($socket, 512)."<br/>";socket_write($socket,"DATArn");echo socket_read($socket, 512)."<br/>";$header = "MIME-Version:1.0rn";$header .= "To: ${mailto}rn";$header .= "From: ${user}rn";$header .= "Subject: ${subject}rn";socket_write($socket, $header . "rn" . $body);echo socket_read($socket, 512)."<br/>";socket_write($socket, "rn.rn");echo socket_read($socket, 512)."<br/>";socket_write($socket,"QUITrn");fclose($sock);

未经允许不得转载:PHP100中文网 - 中国第一档PHP资源分享门户 » 凤凰棋牌php使用socket调用http和smtp协议实例小结

赞 (0) 打赏

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

博乐棋牌 凤凰棋牌游戏 凤凰棋牌 凤凰棋牌 凤凰棋牌app 凤凰棋牌app下载 凤凰棋牌 众鑫棋牌 六合宝典 凤凰棋牌app