common.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. use think\facade\Config;
  3. // 应用公共文件
  4. function app_show($code=0,$message="",$data=[]){
  5. $result = ['code'=>$code,"message"=>$message,"data"=>$data];
  6. echo json_encode($result,JSON_UNESCAPED_UNICODE);
  7. }
  8. // 应用公共文件
  9. function error_show($code=0,$message=""){
  10. $result = ['code'=>$code,"message"=>$message];
  11. echo json_encode($result,JSON_UNESCAPED_UNICODE);
  12. }
  13. function OcrByInvoice($post){
  14. $host = Config::get("app");
  15. $url = $host["api_host"]."/Api/ocr";
  16. $curl = curl_init();
  17. curl_setopt($curl, CURLOPT_URL, $url);
  18. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  19. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  20. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  21. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  22. if($post) {
  23. curl_setopt($curl, CURLOPT_POST, 1);
  24. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
  25. }
  26. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  27. $data = curl_exec($curl);
  28. if (curl_errno($curl)) {
  29. return curl_error($curl);
  30. }
  31. curl_close($curl);
  32. return $data;
  33. }
  34. function GetUserInfo($token){
  35. $host = Config::get("app");
  36. $url = $host["api_host"]."/Api/userinfo";
  37. $data=[
  38. "token"=>$token
  39. ];
  40. $response=curl_request($url,$data);
  41. return json_decode($response,true);
  42. }
  43. //参数1:访问的URL,参数2:post数据(不填则为GET),参数3:提交的$cookies,参数4:是否返回$cookies
  44. function curl_request($url,$post=''){
  45. $curl = curl_init();
  46. curl_setopt($curl, CURLOPT_URL, $url);
  47. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  48. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  49. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  50. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  51. if($post) {
  52. curl_setopt($curl, CURLOPT_POST, 1);
  53. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
  54. }
  55. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  56. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  57. $data = curl_exec($curl);
  58. if (curl_errno($curl)) {
  59. return curl_error($curl);
  60. }
  61. curl_close($curl);
  62. return $data;
  63. }