簡體   English   中英

如何對端點進行api調用?

[英]How is an api call made to an endpoint?

我正在嘗試將一個托管支付系統集成到我正在進行的項目中,但是對於這種情況有多么困惑.Newbie

我已經在我的頁面中設置了類似於條帶的其他要求的內聯付款。

現在它與解決托管付款有關,並且這樣做當資金在托管時,我想結算賣方以獲得我需要調用結算終點的資金。 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調用。

看看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();

使用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);

}

只需使用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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM