2

I've researched a lot about PHP cURL and already know a fair amount of Jquery Ajax however what i'm trying to do isn't straight down the line. I'm making an API call using an API Login button that returns a users nickname.

I then make an ajax request to a php file containing the curl paramaters within which i want to pass the users nickname.

RESOLVED

Using data in the AJAX Request i was able to successfully send the javascript variable nickname as a parameter.

The variable was then accessible via $nickname = $_GET['nickname']; in the cURL PHP file.

Thank you to rollstuhlfahrer for providing the solution marked below.


Ajax Request:

var nickname = API.getUserInfo().value.nickname;
$.ajax({
  method: 'GET',
  url: "/curl-php-file.php",
  cache: false,
  data: {
    nickname: nickname, // Sends data as a URL paramater
  }
  success: function(response) {
    // Data
    var data = response.data.data;
    console.log(data);
  },
  error: function(response, val) {
    console.log("AJAX Request Failed");
  }
});

cURL PHP File:

$nickname = $_GET['nickname']; // Gets data from AJAX
require_once("../../../../../wp-load.php");
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://.../?nickname=' . $nickname,
CURLOPT_HTTPHEADER => array(
  'Accept: application/json',
  'Authorization: MYTOKEN'
)
));
$resp   = curl_exec($curl);
wp_send_json_success(json_decode($resp));
curl_close($curl);
exit;
Community
  • 1
  • 1
Matt
  • 2,450
  • 5
  • 30
  • 36
  • Not clear where you have access to this nickname. Step 1 needs more detail – charlietfl Feb 04 '18 at 13:17
  • Prior to the AJAX request i have access to the variable. – Matt Feb 04 '18 at 13:42
  • Where? In client or server side? – charlietfl Feb 04 '18 at 13:43
  • Client side, it's a javascript variable. I have access to it before the AJAX request and am able to pass it as a string in the URL using the answer below by @rollstuhlfahrer however the php script isn't reading it using the _GET procedure – Matt Feb 04 '18 at 13:45
  • Then show the relevant code used to acquire nickname which is basically the point of the first comment – charlietfl Feb 04 '18 at 13:47
  • If u read the comments below.. i'm already able to pass the variable as a paramater using data in the ajax request, the issue is that the php curl file isnt processing it. – Matt Feb 04 '18 at 13:53
  • None of that is shown in question itself – charlietfl Feb 04 '18 at 13:56
  • I've updated the question with the updated code from the suggestions below and for your benefit i've added the line before the ajax request where u can see the variable being set. As mentioned in the original question, user connects the API via login and the variable is stored from the data returned prior to the AJAX request. – Matt Feb 04 '18 at 13:59

1 Answers1

3

Since you are using GET, you can just pass it along as a query parameter:

$.ajax({
  method: 'GET',
  url: "/curl-php-file.php?nickname=" + nickname,
  cache: false,
  success: function(response) {
    // Data
    var data = response.data.data;
    console.log(data);
  },
}

Another option. This has the beauty if built-in character encoding:

$.ajax({
  url: "/curl-php-file.php",
  method: "get", 
  data: { 
    nickname: nickname,
  },

Source

Then in your PHP code, you retrieve the passed parameter:

$nickname = $_GET['nickname'];

And append it to your URL:

CURLOPT_URL => 'https://.../?nickname=' . $nickname,

You should do some checking, if the nickname is a valid one before sending it off via curl.

rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38