SendDataJob.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Jobs;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Foundation\Bus\Dispatchable;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Queue\SerializesModels;
  8. use Illuminate\Support\Facades\Log;
  9. use Illuminate\Console\Command;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Output\ConsoleOutput;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. class SendDataJob extends Command implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. protected $data;
  17. /**
  18. * Create a new job instance.
  19. *
  20. * @return void
  21. */
  22. public function __construct($data)
  23. {
  24. $this->data = $data;
  25. parent::__construct(); // 调用父类的构造函数
  26. }
  27. /**
  28. * Execute the job.
  29. *
  30. * @return void
  31. */
  32. public function handle()
  33. {
  34. $url = '';
  35. $return = $this->sendRequest($url,$this->data);
  36. file_put_contents('charge.txt',json_encode($this->data),8);
  37. $this->echoMessage(new ConsoleOutput());
  38. }
  39. private function sendRequest($url,$data){return true;
  40. $data = json_encode($data);
  41. $header[] = "Content-Type:application/json";
  42. $ch=curl_init($url);
  43. curl_setopt($ch,CURLOPT_POST,1);
  44. curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
  45. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  46. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  47. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  48. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  49. $response=curl_exec($ch);
  50. file_put_contents('job_send.txt',$response.PHP_EOL,8);
  51. $response=json_decode($response,true);
  52. if(curl_errno($ch) ){
  53. sc(curl_error($ch));
  54. }
  55. return $response;
  56. }
  57. protected function echoMessage(OutputInterface $output)
  58. {
  59. $output->writeln('2222222');
  60. }
  61. protected function execute(InputInterface $input, OutputInterface $output)
  62. {
  63. $output->write('22222222');
  64. $output->writeln('2222222');
  65. }
  66. }