helper.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * 添加自定义辅助函数
  4. */
  5. if ( ! function_exists('config_path'))
  6. {
  7. /**
  8. * Get the configuration path.
  9. *
  10. * @param string $path
  11. * @return string
  12. */
  13. function config_path($path = '')
  14. {
  15. return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
  16. }
  17. }
  18. /**
  19. * @param $url 地址
  20. * @param $param 参数
  21. * @param bool $isCheck 是否检查返回结果
  22. * @param string $method 请求方式
  23. * @return array|mixed
  24. * @throws \GuzzleHttp\Exception\GuzzleException
  25. */
  26. function http($url, $param, $isCheck = true, $method = 'post')
  27. {
  28. try {
  29. $client = new \GuzzleHttp\Client();
  30. $response = $client->request($method, $url, $param);
  31. $result = json_decode($response->getBody()->getContents(), true);
  32. if ($isCheck == true) {
  33. if ($result['code'] == 0) {
  34. return $result['data'];
  35. } else {
  36. return [];
  37. }
  38. } else {
  39. return $result;
  40. }
  41. } catch (\Exception $exception) {
  42. return [];
  43. }
  44. }
  45. function generateSign(array $params, $secret_key)
  46. {
  47. unset($params['sign']);
  48. // 将删除参数组中所有等值为FALSE的参数(包括:NULL, 空字符串,0, false)
  49. $params = array_filter($params);
  50. // 按照键名对参数数组进行升序排序
  51. ksort($params);
  52. // 给参数数组追加密钥,键名为 key, 值为签名配置中配置的 secret_key 的值
  53. $params['chxq_key'] = $secret_key;
  54. \Illuminate\Support\Facades\Log::debug($params);
  55. // 生成 URL-encode 之后的请求字符串
  56. $str = http_build_query($params);
  57. $str = urldecode($str);
  58. \Illuminate\Support\Facades\Log::debug($str);
  59. //$str = "address=计算机啊手机壳阿看见手机卡&address_type=1&area_id=2&area_name=西安市&city_id=2&city_name=西安市&contact_mobile
  60. //=18458881890&contact_name=刘德华&province_id=1&province_name=陕西省&uid=0&zipcode=1000000";
  61. // 将请求字符串使用MD5加密后,再转换成大写,并返回
  62. return strtoupper(MD5($str));
  63. }