我们已经将第三方提供的一系列API集成到Divi的子主题中。在函数中。对于该子主题的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\\u wp\\u error检查,并在触发时获取\\u error\\u消息。其结果如下:
<!-- Array ([0]=> http_request_failed): Array([0]=>cURL error 28: Operation timed out after 5000 milliseconds with 0 bytes received) -->
为了提供代码上下文,下面是包装第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 "<!-- CER: ".print_r($response->get_error_codes(),TRUE).": ".print_r($response->get_error_messages(), TRUE)." -->";
}
if ( $response[\'response\'][\'code\'] == 200 ) { <!-- this is line #292 -->
$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公司已确认以下内容:
自部署以来,没有与我们的错误日志匹配的中断,也没有对其API进行任何更改,对成功的API调用没有速率限制,我们还应该寻找什么?这是API问题吗?