tencentim.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // \app\common\model\Test::update(["content"=>json_encode($result)],["id"=>1]);
  23. if ($result === FALSE) {
  24. return array("code" => 500, "msg" => "file_get_contents failed.");
  25. } else {
  26. return json_decode($result[1], true);
  27. }
  28. }
  29. /**
  30. * @param $url
  31. * @param $jsonStr
  32. * @return array
  33. */
  34. function http_post_json_old($url, $jsonStr)
  35. {
  36. $ch = curl_init();
  37. curl_setopt($ch, CURLOPT_POST, 1);
  38. curl_setopt($ch, CURLOPT_URL, $url);
  39. curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
  40. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  41. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  42. 'Content-Type: application/json; charset=utf-8',
  43. 'Content-Length: ' . strlen($jsonStr)
  44. )
  45. );
  46. $response = curl_exec($ch);
  47. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  48. curl_close($ch);
  49. return array($httpCode, $response);
  50. }
  51. function http_post_json($url, $data, $header = '', $timeOut = 0)
  52. {
  53. //初始化curl
  54. $ch = curl_init();
  55. //参数设置
  56. curl_setopt($ch, CURLOPT_URL, $url);
  57. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  58. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  59. curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
  60. curl_setopt($ch, CURLOPT_HEADER, 0);
  61. curl_setopt($ch, CURLOPT_POST, 1);
  62. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  63. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  64. if($header != '') {
  65. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  66. }
  67. $result = curl_exec($ch);
  68. //连接失败
  69. if($result == FALSE) {
  70. //\think\Log::record('[ CURL ] ERROR ' . curl_error($ch)."\n".var_export(debug_backtrace(), true)."\n", 'error');
  71. }
  72. curl_close($ch);
  73. return $result;
  74. }
  75. /**
  76. * $params 请求参数
  77. */
  78. function messageCheck($params) {
  79. $result = $this->http_post_json($this->api_url,$params);
  80. if ($result === FALSE) {
  81. return array("code" => 500, "msg" => "file_get_contents failed.");
  82. } else {
  83. return json_decode($result[1], true);
  84. }
  85. }
  86. }