Place Javascript to PHP variable -- for Plaid
Place Javascript to PHP variable -- for Plaid
我正在尝试通过 Stripe 集成 Plaid。请耐心等待,因为我对 javascript.
不是很熟悉
我想知道如何将 public_token 和 account_ID 放入 PHP 变量中,以便我可以将其包含在 curl 调用中。
JS
<button id='linkButton'>Open Plaid Link</button>
<script src="https://cdn.plaid.com/link/stable/link-initialize.js"></script>
<script>
var linkHandler = Plaid.create({
env: 'tartan',
clientName: 'Stripe / Plaid Test',
key: '[Plaid key]',
product: 'auth',
selectAccount: true,
onSuccess: function(public_token, metadata) {
// Send the public_token and account ID to your app server.
console.log('public_token: ' + public_token); // HOW DO I PLACE THIS IN PHP/curl?**
console.log('account ID: ' + metadata.account_id); // HOW DO I PLACE THIS IN PHP/curl?**
},
});
PHP/CURL
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'https://tartan.plaid.com/exchange_token',
CURLOPT_POST => true,
CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
),
CURLOPT_POSTFIELDS => http_build_query(array(
'client_id' => '[Plaid client ID]',
'secret' => '[Plaid secret]',
'public_token' => '[Plaid Link public_token]', // HOW DO I CREATE A VARIABLE FROM THE ABOVE JS ( console.log('public_token: ' + public_token);) to place here**
'account_id' => '[Plaid Link account_id]', // HOW DO I CREATE A VARIABLE FROM THE ABOVE JS ( console.log('public_token: ' + public_token);) to place here
)),
));
您会想看看使用 XMLHTTP, also known as AJAX. This allows Javascript to make HTTP requests with post or get. If this looks too difficult, you can use the jQuery.ajax 方法(但您需要在 HTML 中包含 jQuery)。然后,您可以将请求指向您的 PHP 页面,您可以在该页面中发出 cURL 请求并通过 _POST 或 _GET 检索值。
示例如下:
XMLHTTP 请求
var url = 'curl.php?token='+public_token+'&acctid='+metadata.account_id;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(xmlhttp.responseText);
}
};
xhttp.open("GET", url, true);
xhttp.send();
JQUERY.AJAX
假设包含 jQuery 且未调用 jQuery.noConflict()。
$.ajax({
method: "GET",
url: "curl.php",
data: {
token: public_token,
acctid: metadata.account_id
}
}).done(function( msg ) {
console.log( msg );
});
上面的两个例子都只是理论上的,未经测试,但我相信它们应该有效。从这里,您应该能够像这样获取 PHP 中的变量:
'public_token' => $_GET['token'],
'account_id' => $_GET['acctid']
我正在尝试通过 Stripe 集成 Plaid。请耐心等待,因为我对 javascript.
不是很熟悉我想知道如何将 public_token 和 account_ID 放入 PHP 变量中,以便我可以将其包含在 curl 调用中。
JS
<button id='linkButton'>Open Plaid Link</button>
<script src="https://cdn.plaid.com/link/stable/link-initialize.js"></script>
<script>
var linkHandler = Plaid.create({
env: 'tartan',
clientName: 'Stripe / Plaid Test',
key: '[Plaid key]',
product: 'auth',
selectAccount: true,
onSuccess: function(public_token, metadata) {
// Send the public_token and account ID to your app server.
console.log('public_token: ' + public_token); // HOW DO I PLACE THIS IN PHP/curl?**
console.log('account ID: ' + metadata.account_id); // HOW DO I PLACE THIS IN PHP/curl?**
},
});
PHP/CURL
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'https://tartan.plaid.com/exchange_token',
CURLOPT_POST => true,
CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
),
CURLOPT_POSTFIELDS => http_build_query(array(
'client_id' => '[Plaid client ID]',
'secret' => '[Plaid secret]',
'public_token' => '[Plaid Link public_token]', // HOW DO I CREATE A VARIABLE FROM THE ABOVE JS ( console.log('public_token: ' + public_token);) to place here**
'account_id' => '[Plaid Link account_id]', // HOW DO I CREATE A VARIABLE FROM THE ABOVE JS ( console.log('public_token: ' + public_token);) to place here
)),
));
您会想看看使用 XMLHTTP, also known as AJAX. This allows Javascript to make HTTP requests with post or get. If this looks too difficult, you can use the jQuery.ajax 方法(但您需要在 HTML 中包含 jQuery)。然后,您可以将请求指向您的 PHP 页面,您可以在该页面中发出 cURL 请求并通过 _POST 或 _GET 检索值。
示例如下:
XMLHTTP 请求
var url = 'curl.php?token='+public_token+'&acctid='+metadata.account_id;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(xmlhttp.responseText);
}
};
xhttp.open("GET", url, true);
xhttp.send();
JQUERY.AJAX
假设包含 jQuery 且未调用 jQuery.noConflict()。
$.ajax({
method: "GET",
url: "curl.php",
data: {
token: public_token,
acctid: metadata.account_id
}
}).done(function( msg ) {
console.log( msg );
});
上面的两个例子都只是理论上的,未经测试,但我相信它们应该有效。从这里,您应该能够像这样获取 PHP 中的变量:
'public_token' => $_GET['token'],
'account_id' => $_GET['acctid']