tencentim.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace tencentim;
  3. class Tencentim
  4. {
  5. private $api_url;
  6. /**
  7. * 构造函数
  8. */
  9. public function __construct($api_url)
  10. {
  11. $this->api_url = $api_url;
  12. }
  13. /**
  14. * $params 请求参数
  15. */
  16. function toSend($receiptdata) {
  17. // 构造请求参数
  18. $params = $receiptdata;
  19. // random int
  20. $params = json_encode($params);
  21. $result = $this->http_post_json($this->api_url,$params);
  22. if ($result === FALSE) {
  23. return array("code" => 500, "msg" => "file_get_contents failed.");
  24. } else {
  25. return json_decode($result, true);
  26. }
  27. }
  28. /**
  29. * @param $url
  30. * @param $jsonStr
  31. * @return array
  32. */
  33. function http_post_json_old($url, $jsonStr)
  34. {
  35. $ch = curl_init();
  36. curl_setopt($ch, CURLOPT_POST, 1);
  37. curl_setopt($ch, CURLOPT_URL, $url);
  38. curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
  39. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  40. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  41. 'Content-Type: application/json; charset=utf-8',
  42. 'Content-Length: ' . strlen($jsonStr)
  43. )
  44. );
  45. $response = curl_exec($ch);
  46. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  47. curl_close($ch);
  48. return array($httpCode, $response);
  49. }
  50. function http_post_json($url, $data, $header = '', $timeOut = 0)
  51. {
  52. //初始化curl
  53. $ch = curl_init();
  54. //参数设置
  55. curl_setopt($ch, CURLOPT_URL, $url);
  56. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  57. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  58. curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
  59. curl_setopt($ch, CURLOPT_HEADER, 0);
  60. curl_setopt($ch, CURLOPT_POST, 1);
  61. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  62. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  63. if($header != '') {
  64. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  65. }
  66. $result = curl_exec($ch);
  67. //连接失败
  68. if($result == FALSE) {
  69. //\think\Log::record('[ CURL ] ERROR ' . curl_error($ch)."\n".var_export(debug_backtrace(), true)."\n", 'error');
  70. }
  71. curl_close($ch);
  72. return $result;
  73. }
  74. /**
  75. * $params 请求参数
  76. */
  77. function messageCheck($params) {
  78. $result = $this->http_post_json($this->api_url,$params);
  79. if ($result === FALSE) {
  80. return array("code" => 500, "msg" => "file_get_contents failed.");
  81. } else {
  82. return json_decode($result[1], true);
  83. }
  84. }
  85. }