Files
fast_response/front/public/__drugsImagesMedicalTaskConsumer.php
Your Name dce091da91 t
2025-03-27 07:52:44 +00:00

109 lines
3.3 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/*
* description
* authorwh
* email
* createTime{2024/11/26} {15:41}
*/
set_time_limit(0);
//var_dump(111);die;
//创建一个循环,每隔三秒钟执行一次,调用接口
try {
while (true) {
//调用接口
$url = "https://ybx_prediagnosis.excn.top/api/Drugsimagesmedicalupload/queryDrugsImagesMedicalTask";
$res = curl_get($url);
//var_dump($res);
if($res['code'] == 200){
$arr_res = json_decode($res['data'],true);
//var_dump($arr_res);die;
//yes终止no不终止
if($arr_res['data'] == 'yes'){
echo "队列消费已终止";
break;
}
sleep(3);
}else{
echo "队列消费失败,已终止";
log_to_write_txt($res);
break;
}
}
}catch (\Exception $e){
log_to_write_txt(['error'=>'队列消费异常',$e->getMessage(),$e->getTraceAsString()]);
}
function curl_get(string $url, int $timeout = 10, $header=[])
{
$header = $header?:array(
'Accept: application/json',
);
$curl = curl_init();
//curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
//curl_setopt($curl, CURLOPT_SSLVERSION, 3);
//设置抓取的url
curl_setopt($curl, CURLOPT_URL, $url);
//设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, 0);
// 超时设置,以秒为单位
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
// 超时设置,以毫秒为单位
// curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
// 设置请求头
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
//设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
//执行命令
$data = curl_exec($curl);
// 显示错误信息
if (curl_error($curl)) {
//print "Error: ".curl_errno($curl).'-' . curl_error($curl);
//返回错误码
return ['code' => curl_errno($curl), 'msg' => curl_error($curl)];
} else {
//关闭句柄
curl_close($curl);
// 返回的内容
return ['code' => 200, 'msg' => 'cURL ok', 'data' => $data];
}
}
function log_to_write_txt($data = 'test', $dirname = '')
{
$root_path = explode('vendor',__DIR__)[0];
$runtime_path = $root_path.'runtime/';
if ($dirname) {
$filepath = $runtime_path . 'log/' . ($dirname).'/'.date('Ymd');//运行时日志
}else{
$filepath = $runtime_path . 'log/'.date('Ym');//运行时日志
}
if (!file_exists($filepath)) {
mkdir($filepath, 0777, true);
}
if($dirname){
$filepath .= '/log' . date('YmdH') . '.txt';
}else{
$filepath .= '/' . date('d') . '_error.log';
}
$ip = request()->ip();
list($t1, $t2) = explode(' ', microtime());
$write_time = date('Y-m-d H:i:s',$t2).' '.sprintf('%.0f',$t1*1000);
$str = "\n" . $write_time . ' | '. $ip.' | ' . request()->baseUrl() . "\n";
$data = is_object($data) || is_array($data) ? $str . json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) : $str . $data;
file_put_contents($filepath, $data, FILE_APPEND);
}