0

i have a controller which will execute based on my customer request, inside that controller function im using an external api (in php curl ) which will return me a unique request number , when my external api fails to return that unique number i will execute the whole controller function again and again until i get the unique number using loop.

so my question is : is there any way to store the execution log of my controller to a file or to db , my log need the data such as : date, time,number of execution , user request .

    {
      $method = $_SERVER['REQUEST_METHOD'];
      if ($method != 'POST') {
        json_output(400, array('status' => '400', 'message' => 'Bad request.'));
      } else  {

        $data = $this->security->xss_clean($this->input->raw_input_stream);
        $request = json_decode($data);

       
          $PassengerReqAirportID                      =       $request->AirportID;
          $PassengerReqNationality                    =       $request->Nationality;


                do{
                  /* To Push Data to external api  */ 
                
                  $curl = curl_init();
                  curl_setopt_array($curl1, array(
                   CURLOPT_URL => 'http://123.com/api',
                   CURLOPT_RETURNTRANSFER => true,
                   CURLOPT_ENCODING => '',
                   CURLOPT_MAXREDIRS => 10,
                   CURLOPT_TIMEOUT => 0,
                   CURLOPT_FOLLOWLOCATION => true,
                   CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                   CURLOPT_CUSTOMREQUEST => 'POST',
                   CURLOPT_POSTFIELDS =>'{
                     "AirportID": "'.$PassengerReqAirportID.'",
                     "Nation":"'.$PassengerReqNationality.'"}',
                  CURLOPT_HTTPHEADER => array('Content-Type: application/json'),));

                  $response = curl_exec($curl);
                  curl_close($curl);

                  $response = json_decode($response,true);
                  $visitID = $response['AccessionNumber'];
  
                }  while (!array_key_exists('AccessionNumber',$response) ||  ($visitID == NULL));

             //   data i need   $visitID```

above is my example code

avoid bad english
  • In a codeigniter project that I work on, we log lots of different kinds of data into MongoDB -- one of the available "non-relational" database options. https://www.mongodb.com/compare/mongodb-mysql There will be many ways that you can do server-side data logging. I fear that this question is a little Too Broad and potentially Opinion-based. – mickmackusa Sep 15 '21 at 07:26
  • As a matter of professional, best practices -- please never manually craft json strings. For example, this is not wise: `'{"AirportID": "'.$PassengerReqAirportID.'", "Nation":"'.$PassengerReqNationality.'"}'`. You many like to add an amount of delay on your re-firing of the curl call --- this looks potentially abusive of your server resources. Since the payload never changes, do you think you should move some of the curl payload declaration above the start of your loop? – mickmackusa Sep 15 '21 at 07:28
  • I prefer your `do {} while()` block, but otherwise, there are some helpful insights on this [CodeReview answer](https://codereview.stackexchange.com/a/212687/141885). These look relevant in terms of error logging: https://stackoverflow.com/q/7285468/2943403 , https://stackoverflow.com/q/51375227/2943403 , https://stackoverflow.com/q/44718982/2943403 , https://stackoverflow.com/q/8868808/2943403 – mickmackusa Sep 15 '21 at 07:37

0 Answers0