Advertisement
Guest User

Untitled

a guest
Jan 8th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.08 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Jobs;
  4.  
  5. use DateTime;
  6. use GuzzleHttp\Psr7\Request;
  7. use GuzzleHttp\Exception\GuzzleException;
  8. use GuzzleHttp\Client;
  9. use Illuminate\Bus\Queueable;
  10. use Illuminate\Queue\SerializesModels;
  11. use Illuminate\Queue\InteractsWithQueue;
  12. use Illuminate\Contracts\Queue\ShouldQueue;
  13. use Illuminate\Foundation\Bus\Dispatchable;
  14. use DB;
  15. use Auth;
  16.  
  17. use App\VideoInterview;
  18.  
  19. class ToneAnalyzer implements ShouldQueue
  20. {
  21.     use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  22.  
  23.     public $tries = 2;
  24.  
  25.     public $text;
  26.  
  27.     /**
  28.      * Create a new job instance.
  29.      *
  30.      * @return void
  31.      */
  32.     public function __construct($text,$user)
  33.     {
  34.         //
  35.         $this->text = $text;
  36.         $this->user = $user;
  37.  
  38.     }
  39.  
  40.     /**
  41.      * Execute the job.
  42.      *
  43.      * @return void
  44.      */
  45.     public function handle()
  46.     {
  47.        
  48.        
  49.         $target_url = 'https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21';
  50.         //var_dump($post);
  51.         # bluemix authentication username
  52.        $username = 'apikey';
  53.    
  54.         # bluemix authentication password
  55.        $password = 'kkkk';
  56.    
  57.         $ch = curl_init();
  58.         curl_setopt($ch, CURLOPT_URL,$target_url);
  59.         curl_setopt($ch, CURLOPT_POST,1);
  60.         curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  61.         'Content-Type: text/plain'
  62.         ));
  63.        
  64.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  65.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  66.         curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  67.         curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
  68.         curl_setopt($ch, CURLOPT_POSTFIELDS,$this->text);
  69.         //curl_setopt($ch, CURLOPT_POSTFIELDS,  json_encode($post, JSON_UNESCAPED_UNICODE));
  70.         curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  71.        
  72.         $result=curl_exec($ch);
  73.         curl_close ($ch);
  74.         echo '<b>result text -> tones : </b>'. $result . '<br><br>' ;
  75.        
  76.         $json = @json_decode($result);
  77.        
  78.         $user = Auth::user()->id;  
  79.         $now = new DateTime();  
  80.  
  81.         $select_tone = DB::table('video_interviews')->select('video_id','user_id')->where('user_id', $this->user->id)->take(1)->orderBy('video_id', 'desc')->get();
  82.  
  83.         foreach($json->document_tone->tones as $result)
  84.         {          
  85.                
  86.             DB::table('tone_analyzer_documents')->insert(
  87.                 ['video_id' => $select_tone[0]->video_id,
  88.                  'user_id' => $select_tone[0]->user_id,
  89.                  'tone_id' => $result->tone_id,
  90.                  'tone_name' => $result->tone_name,
  91.                  'score' => $result->score,
  92.                  'created_at' => $now
  93.                 ]  
  94.             );  
  95.  
  96.         }
  97.        
  98.         foreach($json->sentences_tone as $result)
  99.         {          
  100.        
  101.             if(empty($result->tones)) {
  102.            
  103.                 DB::table('tone_analyzer_sentences')->insert(
  104.                     ['video_id' => $select_tone[0]->video_id,
  105.                      'user_id' => $select_tone[0]->user_id,
  106.                      'sentences_id' => $result->sentence_id,
  107.                      'text' => $result->text,
  108.                      'created_at' => $now
  109.                     ]
  110.                    
  111.                 );
  112.  
  113.                
  114.             }else{
  115.            
  116.                 foreach($result->tones as $result1)
  117.                 {  
  118.  
  119.                     DB::table('tone_analyzer_sentences')->insert(
  120.                         ['video_id' => $select_tone[0]->video_id,
  121.                          'user_id' => $select_tone[0]->user_id,
  122.                          'sentences_id' => $result->sentence_id,
  123.                          'text' => $result->text,
  124.                          'tone_id' => $result1->tone_id,
  125.                          'tone_name' => $result1->tone_name,
  126.                          'score' => $result1->score,
  127.                          'created_at' => $now
  128.                         ]
  129.                     );
  130.  
  131.                 }
  132.  
  133.             }
  134.         }
  135.  
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement