<?php
if (!defined('BASEPATH'))  exit('No direct script access allowed');

class Livros extends CI_Controller{

    public function __construct() {

        parent::__construct();

        //Youtube API

        require_once ('Google/Client.php');

        require_once ('Google/Service/Books.php');  

    }

    
    public function index(){
        
        if($this->input->post('titulo'))
        
        $url = "https://www.googleapis.com/books/v1/volumes?q=".  urlencode($this->input->post('titulo'))."&key=AIzaSyDpK9duRMYIIdch0W6fZj8ZvPvyExLcqqU&country=BR";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $r = json_decode(curl_exec($ch));
        curl_close($ch);
        if($r){
            $data['results'] = $r;
        }
        
        $this->load->view('area_aluno/tela/mostra-livros',$data);
    }
    

    public function __index(){

        $client = new Google_Client();

        $client->setApplicationName("Imazon Livros");

        $apiKey = "AIzaSyDpK9duRMYIIdch0W6fZj8ZvPvyExLcqqU"; // Change this line.

        // Warn if the API key isn't changed.

        if ($apiKey != 'AIzaSyDpK9duRMYIIdch0W6fZj8ZvPvyExLcqqU') {

          echo missingApiKeyWarning();

        }

        $client->setDeveloperKey($apiKey);



        $service = new Google_Service_Books($client);



        $palavra = $this->input->post('titulo');

        $palavra = html_entity_decode($palavra);

        

        $optParams = array('filter' => 'ebooks');

        $data['results'] = $service->volumes->listVolumes($palavra, $optParams);        

        

        $this->load->view('aluno/tela/mostra-livros',$data);

    }

}

