Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Jobs;
- use DateTime;
- use GuzzleHttp\Psr7\Request;
- use GuzzleHttp\Exception\GuzzleException;
- use GuzzleHttp\Client;
- use Illuminate\Bus\Queueable;
- use Illuminate\Queue\SerializesModels;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use DB;
- use Auth;
- use App\VideoInterview;
- class ToneAnalyzer implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- public $tries = 2;
- public $text;
- /**
- * Create a new job instance.
- *
- * @return void
- */
- public function __construct($text,$user)
- {
- //
- $this->text = $text;
- $this->user = $user;
- }
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle()
- {
- $target_url = 'https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21';
- //var_dump($post);
- # bluemix authentication username
- $username = 'apikey';
- # bluemix authentication password
- $password = 'kkkk';
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL,$target_url);
- curl_setopt($ch, CURLOPT_POST,1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: text/plain'
- ));
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
- curl_setopt($ch, CURLOPT_POSTFIELDS,$this->text);
- //curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post, JSON_UNESCAPED_UNICODE));
- curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
- $result=curl_exec($ch);
- curl_close ($ch);
- echo '<b>result text -> tones : </b>'. $result . '<br><br>' ;
- $json = @json_decode($result);
- $user = Auth::user()->id;
- $now = new DateTime();
- $select_tone = DB::table('video_interviews')->select('video_id','user_id')->where('user_id', $this->user->id)->take(1)->orderBy('video_id', 'desc')->get();
- foreach($json->document_tone->tones as $result)
- {
- DB::table('tone_analyzer_documents')->insert(
- ['video_id' => $select_tone[0]->video_id,
- 'user_id' => $select_tone[0]->user_id,
- 'tone_id' => $result->tone_id,
- 'tone_name' => $result->tone_name,
- 'score' => $result->score,
- 'created_at' => $now
- ]
- );
- }
- foreach($json->sentences_tone as $result)
- {
- if(empty($result->tones)) {
- DB::table('tone_analyzer_sentences')->insert(
- ['video_id' => $select_tone[0]->video_id,
- 'user_id' => $select_tone[0]->user_id,
- 'sentences_id' => $result->sentence_id,
- 'text' => $result->text,
- 'created_at' => $now
- ]
- );
- }else{
- foreach($result->tones as $result1)
- {
- DB::table('tone_analyzer_sentences')->insert(
- ['video_id' => $select_tone[0]->video_id,
- 'user_id' => $select_tone[0]->user_id,
- 'sentences_id' => $result->sentence_id,
- 'text' => $result->text,
- 'tone_id' => $result1->tone_id,
- 'tone_name' => $result1->tone_name,
- 'score' => $result1->score,
- 'created_at' => $now
- ]
- );
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement