du9537 2017-01-17 00:29
浏览 19
已采纳

将Javascript放到PHP变量中 - 用于Plaid

I am trying to integrate Plaid via Stripe. Bear with me as I am not very familiar with javascript.

I want to know how I can place in to a PHP variable the public_token and account_ID so that I may include in the curl call.

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
    )),
));
  • 写回答

1条回答 默认 最新

  • duanan1228 2017-01-17 00:49
    关注

    You will want to take a look at using 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 method (but you need to include jQuery in your HTML). You will then be able to point the request to your PHP page in which you make your cURL request and retrieve the values via _POST or _GET.

    Examples as per below:

    XMLHTTP REQUEST

    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

    Assuming jQuery is included and jQuery.noConflict() has not been called.

    $.ajax({
        method: "GET",
        url: "curl.php",
        data: {
            token: public_token,
            acctid: metadata.account_id
        }
    }).done(function( msg ) {
        console.log( msg );
    });
    

    Both examples above are only theoretical and untested, although I believe they should work. From here, you should be able to get the variables in your PHP like so:

    'public_token' => $_GET['token'],
    'account_id' => $_GET['acctid']
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面