Skip to main content

Create rest api to send curl request from codeigniter controller to google cloud for text to speech convert using codeigniter 3.

 

  • Create Google Cloud account https://cloud.google.com/
  • You can create account using existing mail id.
  • Obtain api key for Text to Speech.
  • Create Rest Api to convert text to speech. 
  • We are using cUrl method to send a request.

    Create Api.php and extend REST_Controller 

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

require APPPATH . '/libraries/REST_Controller.php';

class Api extends REST_Controller{

  public function __construct(){
    parent::__construct();
  }

public function index_get() {
  $text = "Hi, how are you doing? Very well thank you. How about you? Not so well. But thank you for asking. How is the weather there? Oh, its very warm out here.";
  $audioContent = $this->generateSpeech($text);
  $this->response(array(
    "status" => 1,
    "message" => "question 1",
    "data" => $audioContent
  ), REST_Controller::HTTP_OK);
}

private function generateSpeech($text) {
  $api_url = "https://texttospeech.googleapis.com/v1/text:synthesize?key=API_Key";
  $post_data = [
      'input' => [
          'text' => $text
      ],
      'voice' => [
          'languageCode' => 'en-IN',
          'name' => 'en-IN-Wavenet-C' // You can choose other voices too
      ],
      'audioConfig' => [
          'audioEncoding' => 'MP3'
      ]
  ];

  $ch = curl_init($api_url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('content-Type: application/json'));
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));

  $response = curl_exec($ch);
  curl_close($ch);
  return json_decode($response, true);
}
}

?>

Comments

Popular posts from this blog

How to secure codeigniter website?

CSRF Protection (Cross-Site Request Forgery) CSRF process of an attacker tricking their victim into unknowingly submitting a request. CodeIgniter provides CSRF protection out of the box, which will get automatically triggered for every non-GET HTTP request, but also needs you to create your submit forms in a certain way. URI Security CodeIgniter contain following character in URI ·          Alpha-numeric text (Latin characters only) ·          Tilde: ~ ·          Per cent sign: % ·          Period: . ·          Colon: : ·          Underscore: _ ·          Dash: - ·          Space Password Handling ·        ...

Building Dynamic Web Applications with CodeIgniter 3

Introduction:                                 CodeIgniter 3 is a powerful PHP framework that simplifies web application development. Its simplicity and flexibility make it a popular choice among developers for creating dynamic and robust web applications. In this blog post, we'll explore some essential concepts and features of CodeIgniter 3 and show you how to get started with building your web applications.  Installation and Setup:   Begin by downloading and installing CodeIgniter 3.  Configure your development environment (e.g., Apache, MySQL, PHP).  Create a new CodeIgniter project structure.  Model-View-Controller (MVC) Architecture:  Understand the MVC design pattern used in CodeIgniter.  Learn how to structure your application using models, views, and controllers.  Explore the benefits of separating application logic.  Routing and URLs:  ...