【问题标题】:PHP - Obtain data from an external script and use it in the current formPHP - 从外部脚本获取数据并以当前形式使用
【发布时间】:2017-06-13 03:17:04
【问题描述】:

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

所以我尝试将我的脚本分开,只放置 html 并使用“动作”表单调用脚本。 但我不知道如何将外部脚本的循环传递给本地表单,也不知道如何传递变量的值。我尝试使用“return”和“Header Location”,但它不起作用。

这是我目前拥有的:

索引.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
  }
?>

好的,上面的代码可以正常工作,但我需要将获取变量的部分外包出去,我将收到它们的部分,如下所示:

->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>

必须以与初始示例相同的方式在同一页面上收集循环结果。 希望大家能理解,先谢谢了。

【问题讨论】:

  • 只是为了清除我的困惑,这两者都在您的插件目录中。对?还是在根文件夹中?
  • 实际上,它们在插件目录中。是的,我所说的外部文件将位于不同的域中。
  • 您可以为此使用 cURL,
  • 我的脚本已经在使用 cURL 从“example1.com/?k=”页面获取数据。问题是如何获取托管在“example.com/script.php”的脚本生成的变量的值

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


【解决方案1】:

在 index.php 中将表单操作 url 更改为 self 页面并在此处执行 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
}
?>

我希望这是你想要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多