作者 李小龙

C端客户端接口转发

@@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
8 namespace App\Http\Controllers\Api; 8 namespace App\Http\Controllers\Api;
9 9
10 use App\Http\Controllers\Controller; 10 use App\Http\Controllers\Controller;
  11 +use App\Models\SyncSubmitTask\SyncSubmitTask;
11 use Illuminate\Http\Exceptions\HttpResponseException; 12 use Illuminate\Http\Exceptions\HttpResponseException;
12 use Illuminate\Http\JsonResponse; 13 use Illuminate\Http\JsonResponse;
13 use Illuminate\Http\Request; 14 use Illuminate\Http\Request;
@@ -24,6 +25,12 @@ class NoticeController extends Controller @@ -24,6 +25,12 @@ class NoticeController extends Controller
24 const SUCCESS = 200; 25 const SUCCESS = 200;
25 const ERROR = 400; 26 const ERROR = 400;
26 const SERVERERROR = 500; 27 const SERVERERROR = 500;
  28 + const TYPEVISIT = "visit";
  29 + const TYPEINQUIRY = "inquiry";
  30 + const TRAFFICZERO = 0;
  31 + const TRAFFICONE = 1;
  32 + const DEVICE_PORT_ONE = 1;
  33 + const DEVICE_PORT_TWO = 2;
27 protected $header = [];//设置请求头参数 34 protected $header = [];//设置请求头参数
28 35
29 /** 36 /**
@@ -110,7 +117,7 @@ public function curlGet($url){ @@ -110,7 +117,7 @@ public function curlGet($url){
110 curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE); 117 curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE);
111 curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, FALSE); 118 curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, FALSE);
112 curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, true); 119 curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, true);
113 - curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, 'GET'); 120 + curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, "GET");
114 curl_setopt($ch1, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 121 curl_setopt($ch1, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
115 $access_txt = curl_exec($ch1); 122 $access_txt = curl_exec($ch1);
116 curl_close($ch1); 123 curl_close($ch1);
@@ -118,6 +125,45 @@ public function curlGet($url){ @@ -118,6 +125,45 @@ public function curlGet($url){
118 } 125 }
119 126
120 /** 127 /**
  128 + * 发送http post请求
  129 + * @param $url
  130 + * @param $data
  131 + * @param array $header
  132 + * @param bool $is_json
  133 + * @return mixed|string
  134 + */
  135 + function httpPost($url, $data, $header = [],$is_json = true)
  136 + {
  137 + if (empty($header)) {
  138 + $header = array(
  139 + "Accept: application/json",
  140 + "Content-Type:application/json;charset=utf-8",
  141 + );
  142 + }
  143 + $ch = curl_init();
  144 + curl_setopt($ch, CURLOPT_URL, $url);
  145 + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  146 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  147 + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  148 + curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  149 + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  150 + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  151 + curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  152 + curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  153 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  154 + $res = curl_exec($ch);
  155 + if (curl_errno($ch)) {
  156 + $error_message = curl_error($ch);
  157 + @file_put_contents(storage_path('logs/error.log'), var_export($error_message, true) . PHP_EOL, FILE_APPEND);
  158 + }
  159 + curl_close($ch);
  160 + if($is_json){
  161 + return json_decode($res, true);
  162 + }
  163 + return trim($res);
  164 + }
  165 +
  166 + /**
121 * A端上传验证代码 167 * A端上传验证代码
122 * @param Request $request 168 * @param Request $request
123 * @return string 169 * @return string
@@ -145,7 +191,7 @@ public function uploadAmpVerifyFile(Request $request) @@ -145,7 +191,7 @@ public function uploadAmpVerifyFile(Request $request)
145 $domain = $request->getHost(); 191 $domain = $request->getHost();
146 192
147 $domain_array = parse_url($domain); 193 $domain_array = parse_url($domain);
148 - $host = $domain_array['host'] ?? $domain_array['path']; 194 + $host = $domain_array['host'] ? $domain_array['path'] : "";
149 $host_array = explode('.',$host); 195 $host_array = explode('.',$host);
150 if(count($host_array) <= 2){ 196 if(count($host_array) <= 2){
151 array_unshift($host_array,'m'); 197 array_unshift($host_array,'m');
@@ -288,32 +334,159 @@ public function deleteDirectory($path) @@ -288,32 +334,159 @@ public function deleteDirectory($path)
288 } 334 }
289 335
290 /** 336 /**
  337 + * 提交
  338 + * @param $request
  339 + * @param string $type
  340 + * @param int $traffic
  341 + * @param array $files
  342 + * @return string
  343 + */
  344 + public function transmit($request, $type = self::TYPEVISIT, $traffic = self::TRAFFICZERO,$files = []){
  345 + if($request->getClientIp() == "127.0.0.1"){
  346 + return $this->success();
  347 + }
  348 + //判断是否是爬虫
  349 + $isReptile = $this->isReptile($request);
  350 + if($isReptile){
  351 + return $this->success();
  352 + }
  353 +
  354 + $data = $request->all();
  355 + if (empty($data)){
  356 + return $this->success();
  357 + }
  358 + $data["device_port"] = $this->userAgentHandle( $request->userAgent(),$data["device_port"] ?? self::DEVICE_PORT_ONE);
  359 + $req["data"] = $data;
  360 + $req["referer"] = $this->visitInfoHandle($data["referrer_url"] ?? $request->header('Referer'));
  361 + $req["domain"] = $request->getHost();
  362 + $req["ip"] = $request->getClientIp();
  363 + $req["user_agent"] = $request->userAgent();
  364 + $req["files"] = $files;
  365 + $req["type"] = $type;
  366 + $req["traffic"] = $traffic;
  367 +
  368 + //转发接口
  369 + $transmitUrl = env("TRANSMIT_URL");
  370 + return $this->httpPost($transmitUrl,json_encode($req));
  371 + }
  372 +
  373 + /**
  374 + * 处理user_agent
  375 + * @param $user_agent
  376 + * @param $device_port
  377 + * @return int
  378 + */
  379 + public function userAgentHandle($user_agent,$device_port){
  380 + if (!in_array($device_port,[self::DEVICE_PORT_ONE,self::DEVICE_PORT_TWO])){
  381 + $device_port = self::DEVICE_PORT_ONE;
  382 + }
  383 + // 头信息中带有这些信息, 代表是手机端, 重置设备类型
  384 + if (preg_match('/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|wap|windowsce|ucweb/', $user_agent)) {
  385 + $device_port = self::DEVICE_PORT_TWO;
  386 + }
  387 + return $device_port;
  388 + }
  389 +
  390 + /**
291 * 客户访问埋点接口 391 * 客户访问埋点接口
292 * @param Request $request 392 * @param Request $request
293 - * @return JsonResponse 393 + * @return string
294 */ 394 */
295 public function customerVisit(Request $request) 395 public function customerVisit(Request $request)
296 { 396 {
297 - $data = $request->all();  
298 - $data["referrer_url"] = !empty($data["referrer_url"]) ? $data["referrer_url"] : "";  
299 - $data["url"] = !empty($data["url"]) ? $data["url"] : "";  
300 - $data["domain"] = !empty($data["domain"]) ? $data["domain"] : "";  
301 - $data["ip"] = $request->getClientIp();  
302 - $data["user_agent"] = $request->userAgent();  
303 - if (empty($data["referrer_url"]) || empty($data["url"]) || empty($data["domain"])){  
304 - return response()->json([  
305 - 'code' => self::SUCCESS,  
306 - 'msg' => 'success',  
307 - ]); 397 + return $this->transmit($request);
308 } 398 }
309 399
310 - //转发data 400 + /**
  401 + * 埋点信息处理
  402 + * @param $referrerUrl
  403 + * @return string
  404 + */
  405 + public function visitInfoHandle($referrerUrl)
  406 + {
  407 + if(preg_match('/google|facebook|bing|yahoo|youtobe|linkedin|messefrankfurt|yandex|tiktok|twitter|instagram|reddit|telegram|pinterest|tumblr/',$referrerUrl)){
  408 + }else if($referrerUrl == null){
  409 + //直访用户
  410 + $referrerUrl = "";
  411 + }else{
  412 + $referrerUrl = "https://www.google.com/";
  413 + }
  414 + return $referrerUrl;
  415 + }
311 416
312 - //埋点成功  
313 - return response()->json([  
314 - 'code' => self::SUCCESS,  
315 - 'msg' => 'success',  
316 - ]); 417 + /**
  418 + * 是否是爬虫访问
  419 + */
  420 + public function isReptile($request): bool
  421 + {
  422 + $agent = $request->header('User-Agent');
  423 + if (!empty($agent)) {
  424 + $spiderSite= array(
  425 + "TencentTraveler",
  426 + "Baiduspider+",
  427 + "BaiduGame",
  428 + "Googlebot",
  429 + "msnbot",
  430 + "Sosospider+",
  431 + "Sogou web spider",
  432 + "ia_archiver",
  433 + "Yahoo! Slurp",
  434 + "YoudaoBot",
  435 + "Yahoo Slurp",
  436 + "MSNBot",
  437 + "Java (Often spam bot)",
  438 + "BaiDuSpider",
  439 + "Voila",
  440 + "Yandex bot",
  441 + "BSpider",
  442 + "twiceler",
  443 + "Sogou Spider",
  444 + "Speedy Spider",
  445 + "Google AdSense",
  446 + "Heritrix",
  447 + "Python-urllib",
  448 + "Alexa (IA Archiver)",
  449 + "Ask",
  450 + "Exabot",
  451 + "Custo",
  452 + "OutfoxBot/YodaoBot",
  453 + "yacy",
  454 + "SurveyBot",
  455 + "legs",
  456 + "lwp-trivial",
  457 + "Nutch",
  458 + "StackRambler",
  459 + "The web archive (IA Archiver)",
  460 + "Perl tool",
  461 + "MJ12bot",
  462 + "Netcraft",
  463 + "MSIECrawler",
  464 + "WGet tools",
  465 + "larbin",
  466 + "Fish search",
  467 + "yandex.com/bots",
  468 + "google.com/bot",
  469 + "bingbot",
  470 + "YandexMobileBot",
  471 + "BingPreview",
  472 + "AhrefsBot",
  473 + "bot"
  474 + );
  475 + $flag = 0;
  476 + foreach($spiderSite as $val) {
  477 + $str = strtolower($val);
  478 + if (strpos($agent, $str) !== false) {
  479 + $flag = 1;
  480 + }
  481 + }
  482 + if($flag == 1){
  483 + return true;
  484 + }else{
  485 + return false;
  486 + }
  487 + } else {
  488 + return false;
  489 + }
317 } 490 }
318 491
319 /** 492 /**
@@ -323,40 +496,17 @@ public function customerVisit(Request $request) @@ -323,40 +496,17 @@ public function customerVisit(Request $request)
323 */ 496 */
324 public function trafficVisit(Request $request) 497 public function trafficVisit(Request $request)
325 { 498 {
326 - //获取参数  
327 - $data = $request->all();  
328 - $data["id"] = $request->input('ip');  
329 - $data["url"] = $request->input('url');  
330 - $data["device_port"] = intval($request->input('device_port'));  
331 - $data["referrer_url"] = $request->input('referrer_url');  
332 - $data["user_agent"] = $request->input('user_agent');  
333 - if (empty($data["id"]) || empty($data["url"]) || empty($data["referrer_url"]) || empty($data["user_agent"])){  
334 - return response()->json([  
335 - 'code' => self::SUCCESS,  
336 - 'msg' => 'success',  
337 - ]);  
338 - }  
339 -  
340 - //转发data  
341 -  
342 - //成功  
343 - return response()->json([  
344 - 'code' => self::SUCCESS,  
345 - 'msg' => 'success',  
346 - ]);  
347 -  
348 - 499 + return $this->transmit($request,self::TYPEVISIT,self::TRAFFICONE);
349 } 500 }
350 501
351 /** 502 /**
352 * C端表单提交 询盘信息 503 * C端表单提交 询盘信息
353 * @param Request $request 504 * @param Request $request
354 - * @return mixed 505 + * @return string
355 */ 506 */
356 public function inquiry(Request $request) 507 public function inquiry(Request $request)
357 { 508 {
358 $data = $request->all(); 509 $data = $request->all();
359 -  
360 $black_ips = ['203.86.233.27', '37.19.221.202']; 510 $black_ips = ['203.86.233.27', '37.19.221.202'];
361 if(in_array(request()->getClientIp(), $black_ips)){ 511 if(in_array(request()->getClientIp(), $black_ips)){
362 $this->success(); 512 $this->success();
@@ -381,40 +531,26 @@ public function inquiry(Request $request) @@ -381,40 +531,26 @@ public function inquiry(Request $request)
381 } 531 }
382 532
383 $data["files"] = $request->allFiles(); 533 $data["files"] = $request->allFiles();
384 -  
385 - //转发data  
386 - //返回数据  
387 - $res = "";  
388 -  
389 - return $this->responseA($res); 534 + return $this->transmit($request,self::TYPEINQUIRY,self::TRAFFICZERO,$data["files"]);
390 } 535 }
391 536
392 /** 537 /**
393 * 收集邮箱或者手机号等其他信息 538 * 收集邮箱或者手机号等其他信息
394 * @param Request $request 539 * @param Request $request
395 - * @return mixed 540 + * @return string
396 */ 541 */
397 public function inquiryOtherInfo(Request $request) 542 public function inquiryOtherInfo(Request $request)
398 { 543 {
399 - return $this->inquiry($request); 544 + return $this->transmit($request,self::TYPEINQUIRY);
400 } 545 }
401 546
402 /** 547 /**
403 * 起点表单询盘 548 * 起点表单询盘
404 * @param Request $request 549 * @param Request $request
405 - * @return void 550 + * @return string
406 */ 551 */
407 public function inquiryQd(Request $request){ 552 public function inquiryQd(Request $request){
408 - $data = $request->post();  
409 - $data['submit_ip'] = $data['submit_ip'] ? $data['submit_ip'] : '';  
410 - $data['submit_time'] = $data['submit_time'] ? $data['submit_time'] : '';  
411 - $data['refer'] = $data['refer'] ? $data['refer'] : '';  
412 -  
413 - //转发data  
414 - //返回数据  
415 - $res = "";  
416 -  
417 - return $this->responseA($res); 553 + return $this->transmit($request,self::TYPEINQUIRY);
418 } 554 }
419 555
420 /** 556 /**
@@ -434,6 +570,7 @@ public function search(Request $request) @@ -434,6 +570,7 @@ public function search(Request $request)
434 $data["search_content"] = $data['search']; 570 $data["search_content"] = $data['search'];
435 } 571 }
436 572
  573 + //分页
437 $data["page"] = isset($data['page']) && (int)$data['page'] > 1 ? (int)$data['page'] : 1; 574 $data["page"] = isset($data['page']) && (int)$data['page'] > 1 ? (int)$data['page'] : 1;
438 575
439 //转发data 576 //转发data
1 <?php 1 <?php
2 2
3 -use App\Http\Controllers\Api\InquiryController;  
4 use App\Http\Controllers\Api\NoticeController; 3 use App\Http\Controllers\Api\NoticeController;
5 -use App\Http\Controllers\Cside\PageController;  
6 -use App\Http\Controllers\Cside\Visit\VisitController;  
7 use Illuminate\Support\Facades\Route; 4 use Illuminate\Support\Facades\Route;
8 5
9 /* 6 /*