简体   繁体   中英

How to GET json Data using curl in CodeIgniter

I have been trying to use this paystack api

curl "https://api.paystack.co/bank" \
-H "Authorization: Bearer SECRET_KEY" 
-X GET

but don't know how to go about using using curl which am not really familiar with.

I have tried working my way around it but i get stuck all the time.

defined('BASEPATH') OR exit('No direct script access allowed');

class Bank_details extends CI_Controller {

    public function index()
    {
        $url = "https://api.paystack.co/bank";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt(
          $ch, CURLOPT_HTTPHEADER, [
            'Authorization: Bearer SECRET_KEY']
        );
        $request = curl_exec($ch);
        curl_close($ch);
        var_dump(json_decode($request));
    }

}

with these lines, i am displayed with a message like so:

C:\wamp64\www\main_dir\application\controllers\Bank_details.php:22:null

I think you missed providing JSON content type

curl_setopt($ch, CURLOPT_HTTPHEADER,
     array('Content-Type:application/json',)
  );

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