ConveySample.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3. require_once('ApiBase.php');
  4. class ConveySample extends ApiBase
  5. {
  6. protected $isLogin = false; // 不需要登录信息
  7. public function getDeptSample(){
  8. $samples = $this->input->post('sample');
  9. if(!$samples){
  10. $this->wrong('参数错误');
  11. }
  12. $url = $this->config->item('api_url2')."api/v1/ConveySample/getDeptSample";
  13. $ret = $this->curl_post($url,['sample' => $samples]);
  14. if(empty($ret)){
  15. $this->wrong('请求失败');
  16. }
  17. header('Content-Type:application/json; charset=utf-8');
  18. exit($ret);
  19. }
  20. public function sampleReceive(){
  21. $samples = $this->input->post('sample');
  22. if(!$samples){
  23. $this->wrong('参数错误');
  24. }
  25. $url = $this->config->item('api_url2')."api/v1/ConveySample/sampleReceive";
  26. $ret = $this->curl_post($url,['sample' => $samples]);
  27. if(empty($ret)){
  28. $this->wrong('请求失败');
  29. }
  30. header('Content-Type:application/json; charset=utf-8');
  31. exit($ret);
  32. }
  33. public function cancelSample(){
  34. $samples = $this->input->post('sampleCode');
  35. if(!$samples){
  36. $this->wrong('参数错误');
  37. }
  38. $url = $this->config->item('api_url2')."api/v1/ConveySample/cancelSample";
  39. $ret = $this->curl_post($url,['sampleCode' => $samples]);
  40. if(empty($ret)){
  41. $this->wrong('请求失败');
  42. }
  43. header('Content-Type:application/json; charset=utf-8');
  44. exit($ret);
  45. }
  46. function curl_post($url, $data) {
  47. $ch = curl_init ();
  48. $header = array ("Accept-Charset: utf-8",'Expect:' );
  49. curl_setopt ( $ch, CURLOPT_URL, $url );
  50. curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
  51. curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
  52. curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
  53. curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
  54. curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)' );
  55. curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
  56. curl_setopt ( $ch, CURLOPT_AUTOREFERER, 1 );
  57. curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
  58. // 最好加上http_build_query 转换,防止有些服务器不兼容
  59. curl_setopt ( $ch, CURLOPT_POSTFIELDS, http_build_query ( $data ) );
  60. curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
  61. $result = curl_exec ( $ch );
  62. curl_close ( $ch );
  63. return $result;
  64. }
  65. public function ok($data=array(),$msg='成功',$isNull=0,$isObject=0){
  66. $ret = array(
  67. 'message' => $msg,
  68. 'data' => null,
  69. 'code' => 0
  70. );
  71. if($data && is_array($data)){
  72. if($isNull == 0){
  73. $ret['data'] = $this->change_null(array_change_line_to_hump($data));
  74. }else{
  75. $ret['data'] = array_change_line_to_hump($data);
  76. }
  77. }else{
  78. $ret['data'] = $data;
  79. }
  80. header('Content-Type:application/json; charset=utf-8');
  81. if($isObject == 1 && empty($data)){
  82. exit(json_encode($ret, JSON_UNESCAPED_UNICODE|JSON_FORCE_OBJECT));
  83. }else{
  84. exit(json_encode($ret, JSON_UNESCAPED_UNICODE));
  85. }
  86. }
  87. public function wrong($msg = '错误',$code = 1,$data=array(),$isNull=0,$isObject=0){
  88. if($code == 0){
  89. $code = 1; // 一般错误
  90. }
  91. $ret = array(
  92. 'message' => $msg,
  93. 'data' => null,
  94. 'code' => $code
  95. );
  96. if($data){
  97. if($isNull == 0){
  98. $ret['data'] = $this->change_null(array_change_line_to_hump($data));
  99. }else{
  100. $ret['data'] = array_change_line_to_hump($data);
  101. }
  102. }
  103. header('Content-Type:application/json; charset=utf-8');
  104. if($isObject == 1 && empty($data)){
  105. exit(json_encode($ret, JSON_UNESCAPED_UNICODE|JSON_FORCE_OBJECT));
  106. }else{
  107. exit(json_encode($ret, JSON_UNESCAPED_UNICODE));
  108. }
  109. }
  110. }