Common.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Http\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 代理管理模型类
  6. */
  7. class Common extends Model
  8. {
  9. /**
  10. * 接口调用
  11. *
  12. * @access public
  13. * @param mixed $url 接口地址
  14. * @param mixed $where $params参数
  15. * @param mixed $timeout 请求时效
  16. * @return String
  17. */
  18. public function make_request($url, $params, $timeout=30)
  19. {
  20. set_time_limit(0);
  21. if (function_exists('curl_init') === true) {
  22. $ch = curl_init();
  23. $header = array(
  24. 'Accept-Language: zh-cn',
  25. 'Connection: Keep-Alive',
  26. 'Cache-Control: no-cache'
  27. );
  28. curl_setopt($ch, CURLOPT_POST, 1);
  29. curl_setopt($ch, CURLOPT_URL, $url);
  30. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  31. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  32. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  33. if ($timeout > 0) curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  34. $result = curl_exec($ch);
  35. $errno = curl_errno($ch);
  36. curl_close($ch);
  37. return $result;
  38. } else {
  39. $context = array(
  40. 'http' => array(
  41. 'method' => 'POST',
  42. 'header' => 'Content-type: application/x-www-form-urlencoded' . "\r\n" .
  43. 'Content-length: ' . strlen($params),
  44. 'content' => $params));
  45. if ($timeout > 0) $context['http']['timeout'] = $timeout;
  46. $contextid = stream_context_create($context);
  47. $sock = @fopen($url, 'r', false, $contextid);
  48. if ($sock) {
  49. $result = '';
  50. while (!feof($sock)) {
  51. $result .= fgets($sock, 8192);
  52. }
  53. fclose($sock);
  54. } else {
  55. return 'TimeOut';
  56. }
  57. }
  58. return $result;
  59. }//end make_request()
  60. /**
  61. * 接口调用
  62. *
  63. * @access public
  64. * @param mixed $paramsData 接口必要參數
  65. * @param mixed $url 接口地址
  66. * @return String
  67. */
  68. public function sportsAgentParams($paramsData, $url)
  69. {
  70. $paramsSting = '';
  71. $a = 0;
  72. foreach ($paramsData as $key => $value) {
  73. if ($a === 0) {
  74. $paramsSting .= $key . '=' . $value;
  75. } else {
  76. $paramsSting .= '&' . $key . '=' . $value;
  77. }
  78. $a++;
  79. }
  80. $params = base64_encode($paramsSting);
  81. $key = md5($params . session('sportsToken'));
  82. $loginUrl = $url . '?params=' . $params . '&Key=' . $key;
  83. return $loginUrl;
  84. }//end sportsAgentParams()
  85. }