我目前在使用 PHP 编写自动化脚本(无HTML!)。
我有两个 PHP 文件。其中一个执行脚本,另一个接收 $_POST 数据并返回信息。
问题是如何从一个 PHP 脚本发送 POST 请求到另一个 PHP 脚本,获取返回的变量,而不使用 HTML 表单或重定向,并且继续在第一个脚本中进行工作。
我需要在第一个 PHP 文件中多次根据不同条件向另一个文件发出请求,并返回不同类型的数据,具体取决于请求的内容。
我有以下类似代码:
<?php // action.php (first PHP script)
/*
doing some stuff
*/
$data = sendPost('get_info');// send POST to getinfo.php with attribute ['get_info'] and return data from another file
$mysqli->query("INSERT INTO domains (id, name, address, email)
VALUES('".$data['id']."', '".$data['name']."', '".$data['address']."', '".$data['email']."')") or die(mysqli_error($mysqli));
/*
continue doing some stuff
*/
$data2 = sendPost('what_is_the_time');// send POST to getinfo.php with attribute ['what_is_the_time'] and return time data from another file
sendPost('get_info' or 'what_is_the_time'){
//do post with desired attribute
return $data; }
?>
我认为我需要一个函数,该函数将使用属性调用,并发送post请求并基于请求返回数据。 第二个PHP文件:
<?php // getinfo.php (another PHP script)
if($_POST['get_info']){
//do some actions
$data = anotherFunction();
return $data;
}
if($_POST['what_is_the_time']){
$time = time();
return $time;
}
function anotherFunction(){
//do some stuff
return $result;
}
?>
提前感谢你们。
更新:好的。curl方法正在获取php文件的输出。如何只返回$data变量而不是整个输出?