首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SimplePie错误- Wordpress上的卷曲超时

SimplePie错误- Wordpress上的卷曲超时
EN

Stack Overflow用户
提问于 2019-02-08 02:18:52
回答 1查看 1.2K关注 0票数 2

我在我的Wordpress插件中使用fetch_feed()方法得到了error WP HTTP Error: cURL error 28: Operation timed out after 1001 milliseconds with 0 bytes received

这是为了尝试获取更大的RSS提要,我需要增加Curl超时。不确定为什么设置为1秒而不是5秒?

关于这一点的WP文档不是非常详细的WP_Feed_Cache,特别是没有SimplePie_Cache类文档。

如果有任何帮助,我将不胜感激,因为我不确定是否能够连接到SimplePie来增加卷曲超时。另外,我尝试重写我自己的fetch_feed()方法,但没有成功:

代码语言:javascript
复制
    public function fetchFeed( $url ) {
    if( ! class_exists('\SimplePie', false) ) {
        require_once( ABSPATH . WPINC . '/class-simplepie.php' );
    }

    require_once( ABSPATH . WPINC . '/class-wp-feed-cache.php' );
    require_once( ABSPATH . WPINC . '/class-wp-feed-cache-transient.php' );
    require_once( ABSPATH . WPINC . '/class-wp-simplepie-file.php' );
    require_once( ABSPATH . WPINC . '/class-wp-simplepie-sanitize-kses.php' );

    $feed = new \SimplePie();

    $feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );
    // We must manually overwrite $feed->sanitize because SimplePie's
    // constructor sets it before we have a chance to set the sanitization class
    $feed->sanitize = new \WP_SimplePie_Sanitize_KSES();


    /* Customize sanitization */
    $feed->sanitize->enable_cache = false;
    $feed->sanitize->timeout = 60;
    $feed->sanitize->useragent = "Custom Testing Feed Reader";

    $feed->set_cache_class( 'WP_Feed_Cache' );
    $feed->set_file_class( 'WP_SimplePie_File' );

    $feed->set_feed_url( $url );
    $feed->set_timeout( 30 );
    /** This filter is documented in wp-includes/class-wp-feed-cache-transient.php */
    $feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 60, $url ) ); //changing cache time to 60 seconds (instead of 12 hours)
    /**
     * Fires just before processing the SimplePie feed object.
     *
     * @since 3.0.0
     *
     * @param object $feed SimplePie feed object (passed by reference).
     * @param mixed  $url  URL of feed to retrieve. If an array of URLs, the feeds are merged.
     */
    do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
    $feed->init();
    // $feed->set_output_encoding( get_option( 'blog_charset' ) );
    $feed->set_output_encoding( "UTF-8" ); //set statically to UTF-8

    if ( $feed->error() )
        return new \WP_Error( 'simplepie-error', $feed->error() );

    return $feed;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-08 06:05:17

我可以通过使用以下代码来增加Curl超时:

代码语言:javascript
复制
//Set HTTP Request Timeout
add_filter('http_request_args', 'my_http_request_args', 100, 1);
function my_http_request_args( $r ) {
    $r['timeout'] = 30;
    return $r;
}

//Setting WP HTTP API Timeout
add_action('http_api_curl', 'my_http_api_curl', 100, 1);
function my_http_api_curl( $handle ) {
    curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt( $handle, CURLOPT_TIMEOUT, 30 );
}

// Setting custom timeout for the HTTP request
add_filter('http_request_timeout', 'my_custom_http_request_timeout', 101 );
function my_custom_http_request_timeout( $timeLimit ) {
    return 30;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54579820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档