tencentim.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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($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. /**
  52. * $params 请求参数
  53. */
  54. function messageCheck($params) {
  55. $result = $this->http_post_json($this->api_url,$params);
  56. if ($result === FALSE) {
  57. return array("code" => 500, "msg" => "file_get_contents failed.");
  58. } else {
  59. return json_decode($result[1], true);
  60. }
  61. }
  62. }