wordpress - SimplePie 错误 - Wordpress 上的 Curl 超时
问题描述
我WP HTTP Error: cURL error 28: Operation timed out after 1001 milliseconds with 0 bytes received
使用fetch_feed()
我的 Wordpress 插件中的方法遇到错误。
这是为了尝试获取更大的 RSS 提要,我需要增加 Curl Timeout。不知道为什么它也设置为 1 秒而不是 5 秒?
关于此的 WP 文档不是很详细WP_Feed_Cache尤其是SimplePie_Cache
类文档不存在。
任何帮助将不胜感激,不确定我是否能够连接到 SimplePie 以增加 Curl Timeout。另外,我尝试重写我自己的fetch_feed()
方法,但没有成功:
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;
}
解决方案
我可以通过使用以下代码来增加 Curl Timeout:
//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;
}
推荐阅读
- java - 如何编码数组的总和?
- python - 从python中的另一个列表中获取列表的对应元素时使用map函数获取错误列表
- python - 使用 Boto3 Python 库从 AWS 的所有区域聚合实例列表
- firefox - 全屏窗口代码在 Chrome 中工作,在 Firefox 中不工作
- intellij-idea - java代码行、类数、方法数……在intellij中支持插件
- android - 优化 json 响应的解析时间
- java - 如何在 Android 中保存 FCM 令牌?
- git - 基于事件的 Github 模块项目部署
- hdf5 - 是否可以复制整个 hdf5 文件并使用 h5copy 扩展外部链接?
- django - Django zip 使用 xhtml2pdf 生成的多个 PDF 文件