Multiple API Calls in a Class

后端 未结 3 720
情话喂你
情话喂你 2020-12-20 05:29

I am trying to make multiple API requests and I have to make the request in different functions that are within a class like so:

class exampleClass
{    
  f         


        
相关标签:
3条回答
  • 2020-12-20 06:20

    Answer to first question: Yes you are, each time the method is called it executes all its definition again.

    Answer to second question: Yes there is, so called member properties. You can read up about them in the PHP manual here: PHP Manual: Properties

    0 讨论(0)
  • 2020-12-20 06:20

    You are making two API calls, but you don't have to.

    You can put the contents of a call into a member variable in the class with a default value of NULL, and if you want, you can check if that member variable is NULL before making an API call. For example;

    class exampleClass
    {
        private $api_json = NULL;
    
        private function call_api()
        {
            if(is_null($this->api_json))
            {
                $json = // result of api call;
                $this->api_json = $json;
            }
    
            return $this->api_json;
        }
    
        public function printStuffOut() {
            $jsonStuff = $this->call_api();
            $jsonStuff->{'result'}[0]->{'fieldName'};
        }
    
        public function printStuffOut2() {
            $jsonStuff = $this->call_api();
            $jsonStuff->{'result'}[0]->{'fieldName'};
        }
    }
    
    0 讨论(0)
  • 2020-12-20 06:22

    You can use following class to achieve multiple API simultaneously/instantly/at once.

    Click here to get a class.

    How to use it?

    Step 1: Get object.

    //SANI: GET DATA    
    $obj = new multiapi();
    

    Step 2: Make a multiple GET Requests.

    $obj->data = array(YOUR_URL_1,YOUR_URL_2, OUR_URL_3);
    $result = $obj->get_process_requests();
    print_r($result);
    

    Step 3: Make a multiple HTTP POST Requests.

    //SANI: Request with params only
    $obj->data[0]['url']             = 'YOUR_URL_ONE';
    $obj->data[0]['post']                = array();
    $obj->data[0]['post']['param_1']     = 'param_value_1';
    $obj->data[0]['post']['param_2']     = 'param_value_2';
    
    0 讨论(0)
提交回复
热议问题