简体   繁体   English

如何对端点进行api调用?

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

I am trying to integrate an escrow payment system in a project i am working on but kind of confused about how this goes.Newbie 我正在尝试将一个托管支付系统集成到我正在进行的项目中,但是对于这种情况有多么困惑.Newbie

I have already set up the inline payment in my page along other requirements similar to stripe. 我已经在我的页面中设置了类似于条带的其他要求的内联付款。

Now it has to do with settling an escrow payment and to do that When the funds are in escrow and I would like to settle the seller for the funds i would need to call the settlement endpoint. 现在它与解决托管付款有关,并且这样做当资金在托管时,我想结算卖方以获得我需要调用结算终点的资金。 https://ravesandboxapi.flutterwave.com/v2/gpx/transactions/escrow/settle This is a sample request 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.
}

and this is a sample response 这是一个示例响应

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

How exactly is the api call made using the above. 如何使用上面的api调用。

Take a look at Guzzle. 看看Guzzle。 It's a pretty simple PHP package to handle API routing. 这是一个非常简单的PHP包来处理API路由。

http://docs.guzzlephp.org/en/stable/ 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();

When using PHP I always use a function like this one I'm sharing which I find quite useful. 使用PHP时,我总是使用像我这样的函数,我发现它非常有用。 I think it's quite self explanatory but in case you need further assistance please let me know. 我认为这是非常自我解释,但如果您需要进一步的帮助,请告诉我。

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

}

just use guzzle library. 只需使用guzzle库。 i made a simple boilerplate just for API call i used in any project 我为任何项目中使用的API调用做了一个简单的样板

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

Sample use 样品使用

$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