小编Ron*_*nen的帖子

仅使用PHP发送类似AJAX的发布请求

我目前正在研究PHP中的一些自动化脚本(没有HTML!).我有两个PHP文件.一个是执行脚本,另一个是接收$ _POST数据并返回信息.问题是如何从一个PHP脚本发送POST到另一个PHP脚本,获取返回变量并继续处理第一个没有HTML表单且没有重定向的脚本.我需要在不同的条件下从第一个PHP文件到另一个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; }
?>
Run Code Online (Sandbox Code Playgroud)

我想我需要一些将使用属性调用的函数,发送post请求并根据请求返回数据.第二个PHP文件:

<?php // …
Run Code Online (Sandbox Code Playgroud)

php algorithm function http-post

3
推荐指数
1
解决办法
9619
查看次数

循环内循环

我有这项艰巨的任务(至少对我而言).这是抽象的问题,数学我会说.我们假设我有2个输入:

  1. 一个关键字(字符串)
  2. 一个数字(深度级别)

并提交按钮.

此关键字从数据库中返回8个与此字符串类似的其他关键字.对于8个关键字中的每一个,我需要调用相同的函数,这将返回我已经返回的所有这8个字符串中的另外8个类似关键字.这是"级别"号码.我需要在每个返回的字符串中更深入,具体取决于我输入的级别号.

例如:如果级别编号为2,那么我们将调用该函数9次.第一次为原始关键字,每次返回关键字为8次.如果级别编号为3,则该函数将被调用73次.与上一个示例中一样,但是我们已经返回了另外8个关键字.我认为循环内部会有几个循环,但我自己无法弄明白.非常感谢您的建议.

这是我写的主要代码,可能效率不高:

$keywords = preg_split('/$\R?^/m', trim($_POST['keyword']));
$keywords = array_map('trim', $keywords);
$level = $_POST['level'];
if (!$level || $level < 2) {
    echo '<b>Level was either 1 or null</b>';
}
foreach ($keywords as $keyword) {
    $results = getResults($keyword);
    if ($level && $results) {
        for ($i = 0; $i < sizeof($results); $i++) {
            $results1 = getResults($results[$i]);
            for ($j = 0; $j < $level; $j++) {
                $results1 = getResults($results1[$i])
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

输出应该是这样的:

1->
   2
   ->
      3
      3 …
Run Code Online (Sandbox Code Playgroud)

php math loops

0
推荐指数
1
解决办法
161
查看次数

标签 统计

php ×2

algorithm ×1

function ×1

http-post ×1

loops ×1

math ×1