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;