我们已经将第三方提供的一系列API集成到了Divi的子主题中。在该子主题的functions.php中,我们创建了一系列函数,用于生成短代码,以便在模板的各个区域从API返回数据的某些部分。
这些职能的代码是在2019年第三季度编写的。没有对源代码进行任何更改,但截至1月中旬,我们经常(每天几次)出现PHP致命错误,如下所示:
PHP Fatal error: Uncaught Error: Cannot use object of type WP_Error as array in /nas/content/live/client/wp-content/themes/theme/functions.php:292
Stack trace:
#0 /nas/content/live/client/wp-includes/shortcodes.php(325):
get_one_job_title('', '', 'get_one_j...')
#1 [internal function]: do_shortcode_tag(Array)
#2 /nas/content/live/client/wp-includes/shortcodes.php(199): preg_replace_callback('/\\\\[(\\\\[?)(get_on...', 'do_shortcode_ta...', '[get_one_...')
#3 /nas/content/live/client/wp-content/themes/theme/functions.php(716): do_shortcode('[get_one_...')
#4 /nas/content/live/client/wp-includes/class-wp-hook.php(290): client_shortcode_titles('[get_one_...')
#5 /nas/content/live/client/wp-includes/plugin.php(206): WP_Hook->apply_filters('[get_one_...', Array)
#6 /nas/content/live/client/wp-includes/general-template.php(1345): apply_filters('single_post_tit...', '[get_one_...', Object(WP_Post))
#7 /nas/content/live/client/wp-content/themes/divi/epanel/custom_functions.php(1094): single_pos in /nas/content/live/client/wp-content/themes/theme/functions.php on line 292
为了进一步了解正在发生的事情,我们使用了is_wp_error和get_error_messages检查(如果触发的话)。其结果如下:
为了提供代码上下文,下面是包装第292行的函数:
function get_one_job_title() {
global $ApiUrl;
global $ApiAuth;
$position_id = get_query_var( 'position_id' );
$get_url = $ApiUrl . '/reqs/' . $position_id;
if ( preg_match( '/^c-(.*)$/i', $position_id, $matches ) ) {
$position_id = htmlspecialchars_decode( $matches[1] );
$get_url = $ApiUrl . '/reqs/custom/' . $position_id;
}
$headers['API-Realm'] = 'CCAPI';
$headers['Authorization'] = $ApiAuth;
$request = new WP_Http();
$response = $request->request( $get_url, array(
'method' => 'GET',
'headers' => $headers
) );
$return = "";
if(is_wp_error($response)) {
print "";
}
if ( $response['response']['code'] == 200 ) {
$data = json_decode( $response['body'] );
$return .= $data->JobTitle;
} else {
//$return .= "Error: ".__LINE__;
$return .= "No Matching Jobs";
}
return $return;
}
下面是包装#716行的函数:
add_filter( 'the_title', 'client_shortcode_titles' );
function client_shortcode_titles( $title ){
return do_shortcode( $title );
}
add_filter( 'single_post_title', 'client_shortcode_titles' );
主机已经声明,他们不认为这是他们的服务器的问题,并相信问题属于第三方API。空气污染指数公司已证实以下情况:
我们还应该找什么?这是API问题吗?
发布于 2020-02-17 21:57:12
很多可能的原因显而易见-- DNS,路由,防火墙,他们的服务器或应用程序的问题,或者你的服务器或应用程序的问题。
如果他们对api主机名使用循环DNS查询,那么可能会有一个api端点被破坏,当请求到达那个端点时,您的请求就会失败。这个问题并不少见,而且相当容易识别。对于较小的服务,它们可能只对所有请求使用一个IP,这不太可能是一个问题。
你能在WP/PHP之外重现失败吗?创建一个简单的、非PHP的命令行脚本,该脚本记录api服务器主机名的DNS查询的输出和总时间,以及通过curl (linux cli curl -而不是PHP curl)或wget对api的简单GET请求的输出和总时间。跑几次-有用吗?经常从cron运行它-它也偶尔失败吗?这些失败与wordpress的请求也失败的时间一致吗?
此外,记录PHP的详细输出可能会有所帮助。也许可以尝试复制函数WP_Http_Curl::request https://developer.wordpress.org/reference/classes/wp_http_卷曲/请求/
以functions.php作为request_debug()。在该函数中,启用CURLOPT_VERBOSE
并使用CURLOPT_STDERR
捕获输出,如下所述:https://stackoverflow.com/questions/3757071/php-debugging-curl
在插件中使用request_debug(),看看它捕获了什么。
HTH
https://wordpress.stackexchange.com/questions/358883
复制相似问题