doujia7162 2012-09-13 11:56
浏览 54
已采纳

使用WordPress Transient API保存网页

I'm struggling to create web page caches with the Transient API. When I try to save the whole source code, something goes wrong. I'm suspecting that WordPress internally correct the format. In that case, is there a good way to save data intact?

The below code is ready to run as a plugin and demonstrates the problem. When the cache is not created, it shows a web page but once it is created and tries to show the cached data, a blank page is displayed.

<?php
/* Plugin Name: Sample Transient */

add_action('admin_menu', 'sample_transient_menu');
function sample_transient_menu() {
    add_options_page(
        'Sample Transient', 
        'Sample Transient', 
        'manage_options',
        'sample_transient', 
        'sample_transient_admin');
}
function sample_transient_admin() {
    ?>
    <div class="wrap">
    <?php

        $strTransient = 'sample_transient_html';
        $html = get_transient($strTransient);       
        if( false === $html ) {
            echo 'cache is not used: ' . $strTransient . '<br />';      
            $html = wp_remote_get('http://www.google.com'); 
            $html = $html['body'];
            // $html = '<div>hello world</div>'; // <-- this works fine
            set_transient($strTransient, htmlspecialchars($html), 60 );

            $tmp = get_transient($strTransient);
            if (false === $tmp)
                echo 'transient is not saved.';
            else
                echo 'transient is now saved: ' . $strTransient;    
        } else 
            echo 'cache is used. <br />';
        print_r(htmlspecialchars_decode($html));
    ?>
    </div>
    <?php
}

[Edit] Also, it would be appreciated if somebody can provide a solution for the encoding problem. Currently it just shows broken characters.

  • 写回答

1条回答 默认 最新

  • dpjr86761 2012-09-13 22:49
    关注

    Encrypting the strings just worked. I'm not sure about the speed though.

    <?php
    /* Plugin Name: Sample Transient */
    
    add_action('admin_menu', 'sample_transient_menu');
    function sample_transient_menu() {
        add_options_page(
            'Sample Transient', 
            'Sample Transient', 
            'manage_options',
            'sample_transient', 
            'sample_transient_admin');
    }
    function sample_transient_admin() {
        ?>
        <div class="wrap">
        <?php
    
            $strTransient = 'sample_transient_html3';
            $key = 'sample_transient';
            $html = get_transient($strTransient);   
            if( false === $html ) {
                echo 'cache is not used: ' . $strTransient . '<br />';      
                $html = wp_remote_get('http://www.google.com'); 
                $html = $html['body'];  
                $savehtml = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $html, MCRYPT_MODE_CBC, md5(md5($key))));
                set_transient($strTransient, $savehtml, 60 );
            } else {
                echo 'cache is used. <br />';
                $html = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($html), MCRYPT_MODE_CBC, md5(md5($key)));  
            }
            print_r($html);
        ?>
        </div>
        <?php
    }
    

    Reference: Best way to use PHP to encrypt and decrypt passwords?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测