1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- namespace App\Jobs;
- use App\Service\ClearDataService;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Console\Command;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\ConsoleOutput;
- use Symfony\Component\Console\Output\OutputInterface;
- class SendDataJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- protected $data;
- /**
- * Create a new job instance.
- *
- * @return void
- */
- public function __construct($data)
- {
- $this->data = $data;
- }
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle()
- {
- file_put_contents('record2.txt',json_encode($this->data) .PHP_EOL,8);
- //数据要发送的地址
- $url = ClearDataService::getUrl($this->data);
- if(! empty($url)){
- $return = $this->sendRequest($url,$this->data);
- }
- // file_put_contents('send.txt',json_encode($return) .PHP_EOL,8);
- //输出信息 测试
- $this->echoMessage(new ConsoleOutput());
- }
- private function sendRequest($url,$data){
- $data = json_encode($data);
- $header[] = "Content-Type:application/json";
- $ch=curl_init($url);
- curl_setopt($ch,CURLOPT_POST,1);
- curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
- curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
- curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- $response=curl_exec($ch);
- $response=json_decode($response,true);
- if(curl_errno($ch) ){
- sc(curl_error($ch));
- }
- return $response;
- }
- protected function echoMessage(OutputInterface $output)
- {
- $output->writeln('发送结束');
- }
- }
|