skip to Main Content

I would like to send a url with a few additional query parameters. In a nutshell, I would like to make a GET request with a structure similar to this:

https://www.example.com/api?command=<Get>&<partnerName>=<SAMPLE PARTNER NAME>&<authToken>=<AUTH TOKEN>&<returnValueList>=<TYPE OF RETURN>

Here is what I have on ajax:

function getReq(token){
    console.log('get Req triggered in JS')
    jQuery.ajax({
        type: "GET",
        url: "proxy.php?url=https://www.example.com/api?command=Get"+"&"+"partnerName=test"+"&"+"authToken="+token+"&"+"returnValueList="+"transactionList",
        dataType: "json",
        contentType: "application/x-www-form-urlencoded",
        data: token,
        success: function (data){
            console.log('get success', data)
        }
    })
}

This is what I have in proxy.php:

function makeGetRequest($baseURL) {
    print_r('make Get Request triggered');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $baseURL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $response = curl_exec($ch);
    curl_close($ch);  
    if($e = curl_error($ch)) {
        echo $e;
    } else {
      echo $response;
    }
}

The response I get indicates that I am not sending my query params correctly.

UPDATE I added the parameters to a data object:

function getReq(token){
    console.log('get Req triggered in JS')
    jQuery.ajax({
        type: "GET",
        url: "proxy.php?url=https://www.example.com/api?command=Get",
        data: {partnerName: 'applicant', authToken: token, returnValueList: 'transactionList'},
        success: function (data){
            console.log('get success', data)
        }
    })
}

this is what I have in proxy.php now:

$fullURL = $baseURL+'&partnerName='+$_GET['partnerName']+'&authToken='+$_GET['authToken']+'&returnValueList='+$_GET['returnValueList'];
print_r($fullURL);

I am getting this error:

127.0.0.1:59256 [500]: GET /proxy.php?url=https://www.example.com/api?command=Get&partnerName=***&authToken=***&returnValueList=transactionList - Uncaught TypeError: Unsupported operand types: string + string

2

Answers


  1. Maybe you could try passing the parameters by adding them to your ajax request like that:

    data: { 
        firstParam: firstParamValue, 
        secondParam: secondParamValue, 
    }
    

    And removing them from url.

    Get them after with PHP:

    $firstParam = $_GET['firstParam'];
    $secondParam = $_GET['secondParam'];
    
    Login or Signup to reply.
  2. The data: parameters are used as parameters to proxy.php, not the URL that’s embedded in the url= parameter. So you’ll need to create that first, then pass that as the url parameter in data:.

    function getReq(token) {
      console.log('get Req triggered in JS')
      let params = $.param({
        command: 'Get',
        partnerName: 'applicant',
        authToken: token,
        returnValueList: 'transactionList'
      });
      jQuery.ajax({
        type: "GET",
        url: "proxy.php",
        data: {
          url: `https://www.example.com/api?${params}`
        },
        success: function(data) {
          console.log('get success', data)
        }
      })
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search