2017-06-13 21 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 
} 
?> 

我希望這是你想要的。

相關問題