如何对端点进行api调用?

时间:2019-05-08 12:52:36

标签: javascript php json laravel flutterwave

我正在尝试将代管付款系统集成到我正在从事的项目中,但对此工作方式还是感到困惑。新手

我已经按照其他类似条纹的要求在页面中设置了内联支付。

现在,这与结算托管付款有关,并且做到这一点。当资金处于托管状态时,我想用资金结清卖方,我需要致电结算端点。 https://ravesandboxapi.flutterwave.com/v2/gpx/transactions/escrow/settle 这是一个示例请求

{
    "id": "348813", // this is the txid value returned in the v2/verify response.
    "secret_key": "FLWSECK-*************************-X" // your merchant secret key.
}

这是示例响应

{
    "status": "success",
    "message": "SUCCESS",
    "data": "Transaction settled"
}

使用上述方法进行api调用的精确度如何。

3 个答案:

答案 0 :(得分:0)

看看Guzzle。这是一个非常简单的PHP包,用于处理API路由。

http://docs.guzzlephp.org/en/stable/

$client = new GuzzleHttp\Client(['base_uri' => 'https://ravesandboxapi.flutterwave.com']);

$response = $client->request('GET', 
  '/v2/gpx/transactions/escrow/settle',
  ['json' => ['id' => '348813', 'secret_key' => 'KEY']
);

$body = $response->getBody();
$responseData = $body->getContents();

答案 1 :(得分:0)

在使用PHP时,我总是使用这样的函数,我正在共享它,我发现它非常有用。我认为这很容易解释,但是如果您需要进一步的帮助,请告诉我。

function CallAPI($method, $url, $data, $id, $secret_key) {

    $headers = [
        "Content-type: application/json",
        "id: ".$id,
        "secret_key: " .$secret_key
    ];

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    switch ($method) {
        case "GET":
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
            break;
        case "POST":
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            break;
        case "PUT":
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
            break;
        case "DELETE":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
            break;
    }

    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_HEADER         => false,    //return headers in addition to content
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 30,       // timeout on connect
        CURLOPT_TIMEOUT        => 30,       // timeout on response
        CURLOPT_NOBODY        =>  0,        // timeout on response
        CURLOPT_MAXREDIRS      => 9,        // stop after 10 redirects
        CURLINFO_HEADER_OUT    => true,
        CURLOPT_SSL_VERIFYPEER => true,     // Disabled SSL Cert checks
        CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
        CURLOPT_HTTPHEADER      => $headers
    ));

    $result = curl_exec($ch);
            curl_close($ch);
            return json_decode($result, true);

}

答案 2 :(得分:0)

只需使用guzzle库。我为任何项目中使用的API调用制作了一个简单的样板

https://gist.github.com/afiqiqmal/777fc6383ffce11113dc379094ee18b4

样品使用

$result = (new ApiRequest())
     ->baseUrl("https://ravesandboxapi.flutterwave.com")
     ->setHeader([
        'id' => $id,
        'secret_key' => $secretKey
     ])
     ->requestUrl("v2/gpx/transactions/escrow/settle")
     ->fetch();