mercredi 31 juillet 2019

Postman query to an API endpoint which queries another page- possible to have $_SESSION variables on that second page?

I have been using Postman to query a vanilla PHP API, it has been fine, until $_SESSION variables, or just variables in general come into the mix.

The current project setup uses an AJAX query on the front end to set a method, which is then handled by the PHP API.

Take the example of the method "getcounteddates":

var counteddates = $("#counteddates").DataTable({
  responsive: true,
  ajax: {
    url: "ajax_request.php", //Request the data
    dataSrc: function(json) {
      //var old = JSON.stringify(json).replace(/<br\/>/gi, "\"\<\/td\>\<td\>\""); //convert to JSON string
      //var json1 = JSON.parse(old);
      //alert(JSON.stringify(json1));
      return json.data.result; //Return the required data to the datatable
    },
    serverSide: false, //Prepare for when server side processing is turned on
    type: "POST",
    dataType: "json",
    data: function() {
      var data = { method: "getcounteddates" };
      return data;
    }
  },

This will then hit a giant if/else statement in ajax_request.php:

    } else if ($_POST['method'] == 'getcounteddates') {
        $arr = new stdClass();
        //Get array of ESPs
        if (isset($_POST['data'])) {

            $arr->data = json_decode($web_api->Query('/counteddateslist/', 'GET', json_encode(array('data' => $_POST['data']))));
        } else {
            $arr->data = json_decode($web_api->Query('/counteddateslist/', 'GET'));
        }
        echo json_encode($arr);

$web_api is located in api.php, and using the param "/counteddateslist/", will select the function "counteddatesfetch":

    public function counteddatesfetch($path, $method, $query_string)
    {
        if (isset($_SESSION['case_id'])) {
            $caseid = $_SESSION['case_id'];
        }
        list($q, $error) = $this->dbQuerySafe("SELECT  TO_CHAR(TO_DATE(call_date, 'YYYY-MM-DD HH24:MI:SS'), 'DD-MON-YYYY HH24:MI:SS') call_date,
                    TO_CHAR(TO_DATE(call_date -  30, 'YYYY-MM-DD HH24:MI:SS'), 'DD-MON-YYYY HH24:MI:SS')  min_30,
                    TO_CHAR(TO_DATE(call_date - 60, 'YYYY-MM-DD HH24:MI:SS'), 'DD-MON-YYYY HH24:MI:SS')  min_60,
                    TO_CHAR(TO_DATE(call_date - 90, 'YYYY-MM-DD HH24:MI:SS'), 'DD-MON-YYYY HH24:MI:SS')  min_90,
                    TO_CHAR(TO_DATE(call_date - 365, 'YYYY-MM-DD HH24:MI:SS'), 'DD-MON-YYYY HH24:MI:SS')  min_365
                 FROM caseq
                WHERE case_id = :caseid", 's', array(':caseid' => $caseid));
        if (!$error) {
            return array($q, false);
        } else {
            echo 'Error';
            return array(false, $error);
        }
    }

The problem is setting the $caseid. The postman request comes back with "Undefined variable: caseid in /opt/lampp/htdocs/wam_test/api.php on line 1351"

I have tried several options in postman: Manually setting a "Cookie" header to "caseid=170613418". Either setting the same to Variables or Globals in Postmans' environment quick look options.

I believe the problem may be that the request is being sent to ajax_request.php, which then sends a request to api.php.

Api.php is the file which needs access to the caseid variable, but I feel like manually setting it in Postman only gives ajax_request.php access to that variable. Is there any way around this?

Aucun commentaire:

Enregistrer un commentaire