【问题标题】:PHP Simple HTML DOM Parser works on localhost but not live WordPress websitePHP Simple HTML DOM Parser 适用于本地主机但不适用于实时 WordPress 网站
【发布时间】:2014-07-07 22:06:47
【问题描述】:

我正在使用一个 WordPress 插件,它在插件的管理页面上输出一个数据表。我修改了插件,在页面上添加了一个按钮,将数据表导出到 CSV 文件中。在我的本地主机上一切正常,但在传输到实时服务器后,它不再工作 - 导出的 CSV 文件是空白的。我正在使用PHP Simple HTML DOM Parser 来解析 HTML 表并将其转换为 CSV,这似乎是发生障碍的地方。

我添加了带有此代码的导出按钮(部分 URL 为隐私而隐藏):

<form action="**website-url**/wp-content/plugins/**plugin**/includes/votes-logs-export.php" method="POST">
        <input type="submit" value="<?php _e('Export Table as CSV','**plugin**'); ?>" class="button" id="export" name="export" />
</form> 

我的文件 votes-logs-export.php 有这个代码:

require_once('../../../../wp-load.php');
include 'simple_html_dom.php';

$file_name = "contest_votes_".date('d-m-Y-H-i-s').'.csv';
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=".$file_name);

global $wpdb;
$export_sql = "SELECT log.*,pst.post_title FROM ".$wpdb->prefix."votes_tbl as log LEFT JOIN ".$wpdb->prefix."posts as pst on log.post_id=pst.ID ORDER BY pst.post_title DESC";

$export_logs = $wpdb->get_results($export_sql, OBJECT);


if (!empty($export_logs)) {
    $fp = fopen("php://output", "w");
    if ($wpdb->num_rows > 0) {
        $output = '';
        foreach ($export_logs as $logs) {
            $vote_id       = $logs->post_id;                    
            $postdata      = get_posts($vote_id);                 
            $vote_author_id = $postdata[0]->post_author;
            $vote_author   = get_the_author_meta( 'display_name', $vote_author_id );

            $voter_name    = $logs->ip;
            if(filter_var($voter_name, FILTER_VALIDATE_IP) !== false)
                $voter_name = $logs->ip;
            else
                $voter_name = get_the_author_meta( 'display_name', $voter_name );

            $voted_date    = $logs->date;

            $output .= '
                <tr>
                <td>'.$logs->post_title.'</td>
                <td class="author column-author">'.$vote_author.'</td>
                <td class="author column-author">'.$voter_name.'</td>
                <td class="author column-author">'.$voted_date.'</td>
                </tr>';
        }
    }

    $table = '
        <table>
        <tr>
        <th>Title</th>
        <th>Author</th>
        <th>Voter</th>
        <th>Vote Date</th>
        </tr>' . $output . '</table>';

    $html = str_get_html($table);

    foreach($html->find('tr') as $element){
        $td = array();
        foreach( $element->find('th') as $row){
            $td [] = $row->plaintext;
        }
        // prevent blank rows being inserted into CSV file
         if (!empty($td)) {
            fputcsv($fp, $td);
        }
        $td = array();
        foreach( $element->find('td') as $row){
            $td [] = $row->plaintext;
        }
        // prevent blank rows being inserted into CSV file
         if (!empty($td)) {
            fputcsv($fp, $td);
        }
    }
    fclose($fp);
}

我可以通过注释掉标头重定向来查看 PHP 页面以进行调试。在我的本地主机上,PHP 页面以 CSV 格式输出表格数据,但在实时服务器上,它是一个空白页面。 (页面上或 error_log 中没有错误。)我已经调试了这段代码,发现 $table 和上面的所有代码似乎都在完美运行 - 如果我打印 $table 的内容,我会得到一个格式化的 HTML 表我的数据:

<table>
    <tbody>
        <tr>
            <th>Title</th>
            <th>Author</th>
            <th>Voter</th>
            <th>Vote Date</th>
        </tr>
        <tr>
            <td>Writing-on-Stone Park</td>
            <td class="author column-author">admin</td>
            <td class="author column-author">email@email.com</td>
            <td class="author column-author">2014-07-04 16:42:57</td>
        </tr>
        <tr>
            <td>White Tailed Ptarmigan</td>
            <td class="author column-author">admin</td>
            <td class="author column-author">email@email.com</td>
            <td class="author column-author">2014-07-04 06:07:51</td>
        </tr>
        <tr>
            <td>Rockbound Lake</td>
            <td class="author column-author">admin</td>
            <td class="author column-author">email@email.com</td>
            <td class="author column-author">2014-07-04 06:07:28</td>
        </tr>
    </tbody>
</table>

该块发生在$html = str_get_html($table) - 如果我打印出$html,页面上不会打印任何内容,甚至不会出现错误或任何类型的“假”或“空”值。如果我尝试在该代码块之后回显一个字符串,它不会输出到页面,因此脚本似乎停止在那里运行。

我能够确认我的 simple_html_dom.php 文件被正确引用,因为在该文件中回显一个字符串确实将输出到我的 votes-logs-export.php 页面。然而文件中的函数似乎没有被触发?我对这个没有想法!

我确认 'allow_url_fopen' 在 php.ini 中设置为 'true'。在我的 simple_html_dom.php 文件中,我还尝试将“MAX_FILE_SIZE”从 600,000 更改为 100,000,000,但没有效果。服务器上的内存设置为 40MB。我承认我是一个使用自定义 WordPress 插件的新手,但是这在我的本地主机上工作的事实似乎表明实时服务器上存在某种配置或兼容性问题......?如果您能提供任何指导或提示,我将不胜感激。

实时服务器上的 PHP 版本是 5.4.21。我的本地主机使用的是 5.3。

【问题讨论】:

  • 也许 WordPress 的错误处理会干扰这里?尝试 ro reset error_reporting 或设置 error_handler
  • 我猜你的主机关闭了它,因为他们不希望你这样做。如果有的话,试试 curl。
  • @jreuab,我在测试期间的不同时间点都收到了 error_log 中的通知,所以我认为错误报告已打开......但是当我在我的文件中明确打开它时,我终于收到了一个错误! (不确定它是如何决定记录一些错误而不是其他错误...)我收到“调用未定义函数 mb_detect_encoding()”,这显然意味着我需要在我的服务器上启用 mbstring。成功!谢谢大家插话。

标签: php wordpress simple-html-dom export-to-csv


【解决方案1】:

我最终在我的文件中明确设置了error_reporting(1),这最终给我带来了一个有价值的错误 - Call to undefined function mb_detect_encoding() 在 simple_html_dom.php 的第 1238 行。原来我需要在我的服务器上启用 PHP 扩展 mbstring。问题解决了!

【讨论】:

    猜你喜欢
    • 2011-02-16
    • 2016-08-03
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多