PHP - 从外部脚本获取数据并在当前表单中使用它

时间:2017-06-13 03:17:04

标签: php wordpress forms plugins simple-html-dom

我创建了一个脚本,可以作为Wordpress的插件使用,其目的是获取页面视频的数据(视频标题,视频网址,拇指网址和找到的总视频)。 我的插件有效但脚本位于加载结果的同一页面上,因此它保留在Wordpress中。但是我想要负责进行搜索的脚本外部化,因为我真诚地不希望它们能够可视化我的php源代码。

所以我尝试分离我的脚本,然后放入html并使用“action”表单调用脚本。 但我不知道如何将外部脚本的循环传递给本地表单,也不知道如何传递变量的值。我试图使用“返回”和“标题位置”,但它不起作用。

以下是我目前的情况:

的index.php:

<?php
if (isset($_POST['search'])){
$url = $_POST['keyword'];
$parse = "http://example1.com/?k=".$url;
$counter = 0;
$html = dlPage($parse); //dlPage is a function that uses "simple_html_dom"
   include("form_results_footer.php"); // I include the header of the page with the results (this part is outside the loop because I do not want it to be repeated).
foreach ($html->find('something') as $values) {
 //Here I run a series of searches on the page and get the following variables.
    $counter++;
    $title = //something value;
    $linkvideo = //something value;
    $thumburl = //something value;
    include("form_results.php"); //The results of the "foreach" insert them into a separate php in "fieldsets".
}
$totalvideos = $counter;
    include("form_results_footer.php");  //Close the form results
} else {
?>
<html>
  <form action="" method="post">
    <input id="keyword" name="keyword" type="text">
    <button id="search" name="search">Search</button>
  </form>
</html>
<?php
  }
?>

好的,上面的代码工作正常,但是我需要外包我获得部件变量的部分,我会收到它们,如下所示:

- &GT; http://example.com/script.php

<?php
if (isset($_POST['search'])){
$url = $_POST['keyword'];
$parse = "http://example.com/?k=".$url;
$counter = 0;
$html = dlPage($parse); //dlPage is a function that uses "simple_html_dom"
foreach ($html->find('something') as $values) {
 //Here I run a series of searches on the page and get the following variables.
    $counter++;
    $title = //something value;
    $linkvideo = //something value;
    $thumburl = //something value;
}
$totalvideos = $counter;
return $title;
return $linkvideo;
return $thumburl
}
?>

的index.php

<html>
  <form action="http://example.com/script.php" method="post">
    <input id="keyword" name="keyword" type="text">
    <button id="search" name="search">Search</button>
  </form>
</html>

必须以与初始示例相同的方式在同一页面上收集循环结果。 我希望你让我明白,谢谢你。

1 个答案:

答案 0 :(得分:0)

将表单操作URL更改为index.php中的自我页面并在那里调用cURL,调用您放置逻辑的另一个域。这意味着http://example.com/script.php

<强>的index.php

<html>
  <form action="" method="post">
    <input id="keyword" name="keyword" type="text">
    <button id="search" name="search">Search</button>
  </form>
</html>


<?php
if (isset($_POST['search'])) {
    //your URL
    $url = "http://example.com/script.php?keyword=" . $_POST['keyword'];

//  Initiate curl
    $ch = curl_init();
// Disable SSL verification
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
    curl_setopt($ch, CURLOPT_URL, $url);
// Execute
    $result = curl_exec($ch);
// Closing
    curl_close($ch);

// Will dump a beauty json :3
    var_dump(json_decode($result, true));
}
?>

http://example.com/script.php中,您只需将$_POST['keyword']更改为$_GET['keyword'],然后再进行更改即可返回数据。

scrip.php (来自其他域名)

<?php
if (isset($_GET['keyword'])) {
    $url = $_GET['keyword'];
    $parse = "http://example.com/?k=" . $url;
    $counter = 0;
    $html = dlPage($parse); //dlPage is a function that uses "simple_html_dom"

    $return = array(); //initialize return array
    foreach ($html->find('something') as $values) {
        //Here I run a series of searches on the page and get the following variables.
        $return['video_data'][$counter]['title'] = //something value;
        $return['video_data'][$counter]['linkvideo'] = //something value;
        $return['video_data'][$counter]['thumburl'] = //something value;
        $counter++;
    }
    $return['total_video'] = $counter;

    echo json_encode($return); //return data
}
?>

我希望这就是你想要的。