[英]How to call an api using curl in laravel?
我已經使用 php curl 調用了一個 api,我被這個 api 請求格式卡住了,我無法進一步處理,下面是相同的請求示例。
curl -u <YOUR_KEY>:<YOUR_SECRET> \
-X POST https://api.razorpay.com/v1/payouts \
-H "Content-Type: application/json" \
-d '{
"account_number": "7878780080316316",
"fund_account_id": "fa_00000000000001",
"amount": 1000000,
"currency": "INR",
"mode": "IMPS",
"purpose": "refund",
"queue_if_low_balance": true,
"reference_id": "Acme Transaction ID 12345",
"narration": "Acme Corp Fund Transfer",
"notes": {
"notes_key_1":"Tea, Earl Grey, Hot",
"notes_key_2":"Tea, Earl Grey… decaf."
}
}'
如何使用 laravel/PHP 調用這個 api?
您可以直接使用PHP 內置的 cURL 擴展
$username='YOUR_KEY';
$password='YOUR_SECRET';
// set post fields
$post = [
'account_number' => '7878780080316316',
'fund_account_id'=> 'fa_00000000000001',
'amount'=> 1000000,
'currency'=> 'INR',
'mode'=> 'IMPS',
'purpose'=> 'refund',
'queue_if_low_balance'=> true,
'reference_id'=> 'Acme Transaction ID 12345',
'narration'=> 'Acme Corp Fund Transfer',
'notes' =>[
'notes_key_1'=>'Tea, Earl Grey, Hot',
'notes_key_2'=>'Tea, Earl Grey… decaf.',
],
];
$headers = [
'Content-Type: application/json',
];
$ch = curl_init("https://api.razorpay.com/v1/payouts ");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
// do anything you want with your response
var_dump($response);
參考:
您可以使用此代碼在 api 上發布 json:
$url = 'https://api.razorpay.com/v1/payouts';
$ch = curl_init($url);
$data = array(
'username' => 'codexworld',
'password' => '123456'
);
$payload = '{
"account_number": "7878780080316316",
"fund_account_id": "fa_00000000000001",
"amount": 1000000,
"currency": "INR",
"mode": "IMPS",
"purpose": "refund",
"queue_if_low_balance": true,
"reference_id": "Acme Transaction ID 12345",
"narration": "Acme Corp Fund Transfer",
"notes": {
"notes_key_1":"Tea, Earl Grey, Hot",
"notes_key_2":"Tea, Earl Grey… decaf."
}
}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
Guzzle 是一個 PHP 包,它提供了一個干凈且易於使用的接口來執行 HTTP 請求。 您可以考慮使用 Guzzle 而不是 curl。 http://docs.guzzlephp.org/en/stable/
您首先需要將它包含在您的項目中:
composer require guzzlehttp/guzzle
然后可以按照以下方式編寫代碼:
$client = new \GuzzleHttp\Client();
$authentiation = [
'<YOUR_KEY>',
'<YOUR_SECRET>'
];
$postData = [
'account_number'=> '7878780080316316',
'fund_account_id'=> 'fa_00000000000001',
'amount'=> 1000000,
'currency'=> 'INR',
'mode'=> 'IMPS',
'purpose'=> 'refund',
'queue_if_low_balance'=> true,
'reference_id'=> 'Acme Transaction ID 12345',
'narration'=> 'Acme Corp Fund Transfer',
'notes' =>[
'notes_key_1'=>'Tea, Earl Grey, Hot',
'notes_key_2'=>'Tea, Earl Grey… decaf.',
],
];
$response = $client->request('POST',
'https://api.razorpay.com/v1/payouts', [
'auth' => $authentication,
'form_params' => postData,
],
]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.