2017-06-16 161 views
0

我正在製作一個插件,它接收一個表單中輸入的關鍵詞,然後處理這個詞並在網頁中進行搜索,當收到結果時(通過AJAX ),視頻將被屏蔽。PHP - 從另一個域的外部文件調用wordpress函數

在我的script.php而不是回顯所有的html行中,我使用「include」來包含一個php文件(託管在一個外部域中)與html行,然後這些打印在域中使用插件的人。一切正常,但在想要顯示調用Wordpress內部函數的類別列表時,插件失敗,顯然這是因爲腳本試圖從託管文件的域中查找這些函數,而不是從本地wordpress。我怎麼能這樣做?

我的scrypt.js託管在本地域中,即包含在用戶將下載的插件的文件中。正如你所看到的,它可以調用外部域中託管的api.php。

<script> 
     jQuery(function($){ 
      var pluginUrl = '<?php echo plugin_dir_url(__FILE__); ?>' ; 
      $('[id^="form-busqueda"]').on('submit', function(e) { 
       e.preventDefault(); 
       $.ajax({ 
        type : 'POST', 
        url : 'http://example.com/server/api.php', 
        data : $(this).serialize(), 
        cache: false, 
        beforeSend: function(){ 
         $('#results').html('<img src="'+pluginUrl+'../../assets/img/loading.gif" />'); 
        } 
       }).done(function(data) { 
        $('#results').html(data); 
       }); 
      }); 
     }); 
    </script> 

好吧,這個查詢將通過AJAX到另一個域名託管我api.php文件,該文件將與「包括(」 results.php「)」,該文件results.php也迴應裝在相同的外部域哪裏是我的api.php文件

api.php

<?php 
//A series of validations are made, variables are generated (example $variable1, and finally the file "results.php" is called that calls all the variables so that finally the content is printed in the site of the client that is using the plugin. 
include("results.php"); 
?> 

results.php

<div class="container"> 
    <p><?php echo $variable1 ?></p> 
    <p><?php echo $variable2 ?></p> 
    <p><?php echo $variable3 ?></p> 
    <?php 
    $category=addslashes($_GET['cat']); 
    $args = array(
'orderby'   => 'name', 
'order'    => 'ASC', 
'hide_empty'   => 0, 
'selected'   => $category, 
'name'    => 'cat_', 
); 
wp_dropdown_categories($args); 
?> 
</div> 

問題是當我從這個文件中調用Wordpress函數時。但我不知道我還能用什麼。我希望我的問題已經清楚了。提前致謝。

回答

0

在大多數Web服務器(php.ini)中設置爲允許文件包含,因此出於安全原因您不能使用include從遠程地址包含文件。

,或者如果你想讀,雖然遠程文件的HTML內容,你可以利用這一點,在指令allow_url_include必須在php.ini

被設置爲開,那麼對你來說,它可以幫助使用file_get_contents函數代替BUT,這將作爲純HTML標記代碼返回,不會有任何服務器端代碼。

相關問題