Summary of php using socket to call http and smtp protocol instances

  • 2021-12-13 16:26:38
  • OfStack

This article illustrates how php uses socket to invoke http and smtp protocols. Share it for your reference, as follows:

socket sends HTTP request

http Protocol Request Message Format

get


##  Request method   Request file path ? Query string  HTTP/ Protocol version number 
## Host:  Hostname : Port number 
## Connection:close

post


##  Request method   Request file path  HTTP/ Protocol version number 
## Host:  Hostname : Port number 
## Content-type: application/x-www-form-urlencoded
## Content-length :   Query string length 
## Connection: close
##  Query string 

api.php


<?php
echo $_SERVER["REQUEST_METHOD"]."<br/>";
print_r($_REQUEST);

fsocketopen sends get request


<?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);

The sockets extension sends post requests


<?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 sends smtp request

First connect to the smtp server through socket, for example, smtp. 163. com 25, and then implement the necessary parameters for smtp to send mail.

HELO Hostname
Sender mailbox after AUTH LOGIN base64 encoding
Password after base64 encoding
# # Fill out the envelope
MAIL FROM: < Sender's mailbox >
RCPT TO: < Recipient mailbox >
# # Fill in the letter
DATA
MIME-Version:1.0
To: Recipient Mailbox
From: Sender's mailbox
Subject: Message Subject

Mail content

fsocketopen Send Mail

fsocketopen And pfsocketopen Is a function of php mechanism itself, which can be called directly without installing extensions. The above two functions can open a network connection or an Unix socket connection, and then we can use the fwrite , fread , fclose , feof and other functions directly manipulate the network connection.


<?php
$user = "root@163.com";
$password = "root";
$mailto = "1234567@qq.com";
$subject = "fsocketopen Send Mail ";
$body = " Did you get the email, dear ";
$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 Extended Send Mail

The function of sockets extension is more powerful, and tcp/ip server can be built. However, in the example of sending mail using smtp protocol, we only need to use the function of socket client.


 <?php
$user = "root@163.com";
$password = "root";
$mailto = "1234567@qq.com";
$subject = "sockets Expand sending mail ";
$body = " Did you get the email, dear ";
$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);

Reference article:

https://www.ofstack.com/article/66594.htm

https://www.ofstack.com/article/114394.htm

For more readers interested in PHP related content, please check the topics on this site: "php socket Usage Summary", "php String (string) Usage Summary", "PHP Mathematical Operation Skills Summary", "php Object-Oriented Programming Introduction Tutorial", "PHP Array (Array) Operation Skills Complete Book", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary" and "PHP Network Programming Skills Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: