使用PHP下载Twilio VoiceBase高精度转录

时间:2017-08-03 11:16:13

标签: twilio

我正在使用VoiceBase高精度转录插件。我需要使用PHP下载trascripted文本。在我给了webhook URL之后,我得到了有效负载URL的成功响应。

但是,如何使用这些信息在PHP中下载脚本文件。

由于

1 个答案:

答案 0 :(得分:3)

在您的webhook网址

上使用此脚本
<?php

    //First collectand parse JSON from Twilio POST
    $twilio_response_payload = $_POST["AddOns"];
    $twilio_response_payload = json_decode($twilio_response_payload, true);

    //Grab the VoiceBase Data url
    $url = $twilio_response_payload['results']['voicebase_transcription']['payload'][0]['url'];


    $twilio_account_sid = "your-Twilio-sid";
    $twilio_auth_token = "your-Twilio-Auth-Token";


     //Send a GET with basic auth

        set_time_limit(0);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 0);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 0);
        curl_setopt($ch, CURLOPT_LOW_SPEED_LIMIT, 0);
        curl_setopt($ch, CURLOPT_LOW_SPEED_TIME, 60);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERPWD, $twilio_account_sid . ":" . $twilio_auth_token);  
        $response = curl_exec($ch);
        curl_close($ch);

      //Response is of xml form that contains an s3 url, extract the s3 url. I manipulate the string, there is probably a cleaner way.

      $s3url = substr($response, strpos($response, "<RedirectTo>") + 12, strpos($response, "</RedirectTo>") - strpos($response, "<RedirectTo>") - 12);            
      $s3url = html_entity_decode($s3url);


      //Make another curl to that s3 url

        set_time_limit(0);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 0);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 0);
        curl_setopt($ch, CURLOPT_LOW_SPEED_LIMIT, 0);
        curl_setopt($ch, CURLOPT_LOW_SPEED_TIME, 60);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
        curl_setopt($ch, CURLOPT_URL, $s3url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($ch);
        curl_close($ch);              

      //The response will be some JSON, parse the JSON

      $json_response = json_decode($response,true);

      //Media > Transcripts > Text is the plain text transcript, create a file or do whatever you like with it

      $text = $json_response['media']['transcripts']['text'];

      $file = 'transcription.txt';
      file_put_contents($file, $text);

  ?>