首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用PHP实现Twilio VoiceBase高精度抄写下载

用PHP实现Twilio VoiceBase高精度抄写下载
EN

Stack Overflow用户
提问于 2017-08-03 19:16:13
回答 1查看 149关注 0票数 0

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

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

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-05 04:12:24

在您的webhook URL中使用此脚本

代码语言:javascript
复制
<?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);

  ?>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45482630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档