我想在我的脚本中使用fsockopen将一些帖子数据传递给我服务器上的另一个php脚本。我已经对它进行了测试,除非您的网站上有路由,否则实施起来非常简单。
这是我的工作示例(在服务器上没有路由):
socket_post.php
<?php
header('Content-type: text/plain');
$fp = fsockopen('example.com', 80, $errno, $errstr, 30);
$vars = array(
'filename' => 'article.txt',
'data' => 'Hello world'
);
$content = http_build_query($vars);
fwrite($fp, "POST /sockets/receive_data.php HTTP/1.1\r\n");
fwrite($fp, "Host: example.com\r\n");
fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n");
fwrite($fp, $content);
//fclose($fp); // Closes socket immediately without waiting for response
//echo "Done\r\n";
while (!feof($fp)) {
echo fgets($fp, 1024);
}
receive_data.php
<?php
extract($_POST);
sleep(10);
file_put_contents($filename, $data);
因此,它在我的测试服务器上运行良好,但是当我尝试使用OpenCart CMS实现此脚本时,由于OpenCart的控制器路由而失败。
这是OpenCart脚本的一个版本:
socket_post.php
<?php
class ControllerSocketsSocketPost extends Controller {
public function index() {
header('Content-type: text/plain');
$fp = fsockopen("example.com", 80, $errno, $errstr, 30);
$vars = array(
'filename' => 'article.txt',
'data' => 'Hello world'
);
$content = http_build_query($vars);
fwrite($fp, "POST https://example.com/index.php?route=sockets/receive_data HTTP/1.1\r\n"); // !!!THE MAIN PROBLEM HERE!!!
fwrite($fp, "Host: example.com\r\n");
fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n");
fwrite($fp, $content);
//fclose($fp); // Closes socket immediately without waiting for response
//echo 'Done';
while (!feof($fp)) {
echo fgets($fp, 1024);
}
}
}
receive_data.php
<?php
class ControllerSocketsReceiveData extends Controller {
public function index() {
extract($this->request->post);
sleep(10);
file_put_contents($filename, $data);
}
}
因此,它应该在同一目录中创建"article.txt"
文件并在页面上呈现HTTP/1.1 200 OK
状态,但由于路由和html输出{{1},它会获得HTTP/1.1 302 Found
状态}}
在这种情况下如何处理OpenCart的路由?任何建议将不胜感激,谢谢!
答案 0 :(得分:0)
只需要通过localhost建立连接并找到未使用的端口:
<强> socket_post.php 强>
<?php
class ControllerSocketsSocketPost extends Controller {
public function index() {
header('Content-type: text/plain');
$fp = fsockopen('localhost', 8080, $errno, $errstr, 30);
if ($errno) {
echo "ERRNO: $errno \r\n";
echo "ERRSTR: $errstr \r\n";
} else {
$vars = array(
'mail_to' => 'your@email.com',
'message' => 'Hello world'
);
$content = http_build_query($vars);
if ($fp) {
fwrite($fp, "POST /catalog/controller/sockets/receive_data.php HTTP/1.1\r\n");
fwrite($fp, "Host: localhost\r\n");
fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n");
fwrite($fp, $content);
fclose($fp); // Complete script execution without waiting for the response
echo "Done\r\n";
/*
while (!feof($fp)) { // Wait for response if socket connection not closed yet
echo fread($fp, 1024);
}
*/
}
}
}
}
接收器应该只是简单的php脚本,在这种情况下idk如何处理OpenCart控制器:
<强> receive_data.php 强>
<?php
extract($_POST);
sleep(10);
mail($mail_to, 'Async PHP', $message);
希望它会对某人有所帮助。