Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

wordpress - Storing Data in Global Var in PHP

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 getCompanyDeatialsis 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...