优惠券订单及其他脚本

IpAgencyService.php 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hy
  5. * Date: 2019-05-16
  6. * Time: 13:41
  7. */
  8. class IpAgencyService
  9. {
  10. /**
  11. * 获取ip代理
  12. * @return mixed
  13. */
  14. public static function getIpAgency() {
  15. // 再redis中存10秒,在去请求新的代理
  16. $res = RedisModel::get('ipproxyPort');
  17. if($res){
  18. return json_decode($res);
  19. }
  20. $url = 'http://mvip.piping.mogumiao.com/proxy/api/get_ip_bs?appKey=f40abf802fec41f3ab723e0bcf529ab0&count=5&expiryDate=0&format=1&newLine=2';
  21. $result = self::curl('GET',$url);
  22. if (isset($result->code) && $result->code == 0) {
  23. RedisModel::set('ipproxyPort', json_encode($result->msg));
  24. RedisModel::expire('ipproxyPort', 10);
  25. // 之前的代码看的不是特别明白,限制出现了限制直接把返回的代理缓存30秒,如果超过了30秒再去访问
  26. return $result->msg;
  27. }else {
  28. if (isset($result->code) && $result->code == 3001) { //接口频率限制, 每次10s
  29. sleep(1);
  30. return self::getIpAgency();
  31. }
  32. die();
  33. }
  34. }
  35. private static function curl($method, $url, $data=array(), $headers=array()) {
  36. $ch = curl_init();//初始化curl
  37. curl_setopt($ch, CURLOPT_URL,$url);
  38. curl_setopt($ch, CURLOPT_HEADER, 0);//
  39. // curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  40. curl_setopt($ch, CURLOPT_TIMEOUT, 10); //单位 秒,也可以使用
  41. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  42. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
  43. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  44. if($method == 'post'){
  45. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  46. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  47. }
  48. $data = curl_exec($ch);//运行curl
  49. curl_close($ch);
  50. return json_decode($data);
  51. }
  52. }