2017-06-13 20 views
0

我已经创建了一个脚本,它将用作Wordpress的一个插件,其目的是获取页面视频的数据(视频标题,视频url ,拇指网址和总视频)。 我的插件可以工作,但脚本位于加载结果的相同页面上,因此它保留在Wordpress中。但是我想将执行搜索的脚本外化,因为我真诚地不希望它们能够可视化我的PHP源代码。PHP - 从外部脚本中获取数据并以当前形式使用它

所以我试图分开我的脚本,只是把HTML和调用脚本使用“行动”的形式。 但我不知道如何将我的外部脚本的循环传递给本地窗体,也不知道如何传递变量的值。我试图使用“返回”和“标题位置”,但它不起作用。

这是我目前有:

的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 
    } 
?> 

好了,上面的代码工作正常,但我需要外购部分在那里我得到这个角色的变量在哪里将接收他们,这样的事:

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

循环的结果将有以同样的方式在同一页面上收集在最初的例子。 我希望你能让我明白,先谢谢你。

+0

只是为了清楚我的困惑,这两个都在你的插件目录中。对?或在根文件夹? –

+0

实际上,它们位于插件目录中。是的,我所说的外部文件将在不同的领域。 – Kokox

+0

您可以使用cURL, –

回答

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

我希望这是你想要的。

相关问题