I am trying to save an array in global var.
I am calling an API that returns an array and I want to save it once when the code is starting and then use it all over the code.
I forgot to mention that I am building a WordPress plugin.
so this is my code:
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 );
}
}
the getCompanyDeatials
is working, and also the my_run_only_once
is running just once
but I can`t save it into global var ( i tired also to declare global $company
and it didn't work)
Thanks for the help.
question from:
https://stackoverflow.com/questions/65842418/storing-data-in-global-var-in-php 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…