我试图在全局变量中保存一个数组。 我正在调用一个返回数组的 API,我想在代码启动时保存一次,然后在整个代码中使用它。 我忘了提到我正在构建一个 WordPress 插件。 所以这是我的代码:
function getCompanyDeatials()
{
$email = get_option('mail');
$URL = "https://myapi/". "company?email=". $email;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_URL, $URL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json; UTF-8'));
$result = curl_exec($curl);
$var = json_decode($result, true);
$body = $var["body"];
curl_close($curl);
return $body;
}
add_action( 'init', 'my_run_only_once' );
function my_run_only_once() {
if ( did_action( 'init' ) >= 2 )
return;
if( ! get_option('run_create_vip_order_once') ) {
$company = getCompanyDeatials();
upadte_post_meta(0,'company', $company );
update_option( 'run_create_vip_order_once', true );
}
}
getCompanyDeatials
正在运行,而且 my_run_only_once
只运行一次
但我无法将它保存到全局变量中(我也厌倦了声明 global $company
但它没有用)
感谢您的帮助。