简体   繁体   中英

How to call an api using curl in laravel?

I have used php curl to call an api, I am stuck with this api request format I am not able to process further, below is the same request example.

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."
  }
}'

How to call this api using laravel/PHP ?

You can directly use PHP in-built cURL extension for the same

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

References:

https://www.php.net/manual/en/book.curl.php

You can use this code to post json on api:

$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 is a PHP package which provides a clean and easy to work with interface for performing HTTP requests. You might consider using Guzzle instead of curl. http://docs.guzzlephp.org/en/stable/

You would first need to include it in your project:

composer require guzzlehttp/guzzle

And could then write code along these lines:

$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,
        ],
    ]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM