为什么FILE_GET_CONTENTS返回页面源代码?

时间:2017-03-04 作者:jeffbeene

我想在我的主题functions.php 可以在服务器上显示CSV文件内容的文件。

稍后我会担心将CSV文件读入数组等,但现在我只想确保我可以使用file_get_contents. 但是,当我运行下面的短代码时,警报包含HTML。

function display_csv_data_func( $atts ) {
   $file_contents = file_get_contents("http://example.com/something/file.csv");
   return "<script>alert(\'" . $file_contents . "\');</script>";
}
add_shortcode( \'display_csv_data\', \'display_csv_data_func\' );
返回值:

alert(\'
<!doctype html>
<!--[if lt IE 7 ]> <html class="no-js ie6" lang="en-US"
...
是否有关于短代码和函数的内容。php或WordPress通常会干扰我的代码?看起来这应该是一件相当简单的事情。。。

1 个回复
最合适的回答,由SO网友:jeffbeene 整理而成

啊哈,我想出来了!你必须使用wp_remote_get(). 我现在不太清楚为什么,但代码是这样的:

function display_csv_data_func( $atts ) {
   $file_path = "http://example.com/something/file.csv";
   $response = wp_remote_get($file_path);
   $response_body = wp_remote_retrieve_body($response);
   return "<script>alert(\'" . $response_body . "\');</script>";
}
add_shortcode( \'display_csv_data\', \'display_csv_data_func\' );

相关推荐

如何将Java脚本添加到Custom-Page.php模板?

如何将javascript添加到自定义页面。php模板?如何使从w3schools ajax教程获得的以下javascript在自定义页面上工作。php模板?任何帮助都将不胜感激。工作javascript包含在以下HTML中:<!DOCTYPE html> <html> <style> table,th,td { border : 1px solid black; border-collapse: collapse;&#x