PayHttpClient.class.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * http、https通信类
  4. * ============================================================================
  5. * api说明:
  6. * setReqContent($reqContent),设置请求内容,无论post和get,都用get方式提供
  7. * getResContent(), 获取应答内容
  8. * setMethod($method),设置请求方法,post或者get
  9. * getErrInfo(),获取错误信息
  10. * setCertInfo($certFile, $certPasswd, $certType="PEM"),设置证书,双向https时需要使用
  11. * setCaInfo($caFile), 设置CA,格式未pem,不设置则不检查
  12. * setTimeOut($timeOut), 设置超时时间,单位秒
  13. * getResponseCode(), 取返回的http状态码
  14. * call(),真正调用接口
  15. *
  16. * ============================================================================
  17. *
  18. */
  19. class PayHttpClient {
  20. //请求内容,无论post和get,都用get方式提供
  21. var $reqContent = array();
  22. //应答内容
  23. var $resContent;
  24. //错误信息
  25. var $errInfo;
  26. //超时时间
  27. var $timeOut;
  28. //http状态码
  29. var $responseCode;
  30. function __construct() {
  31. $this->PayHttpClient();
  32. }
  33. function PayHttpClient() {
  34. $this->reqContent = "";
  35. $this->resContent = "";
  36. $this->errInfo = "";
  37. $this->timeOut = 120;
  38. $this->responseCode = 0;
  39. }
  40. //设置请求内容
  41. function setReqContent($url,$data) {
  42. $this->reqContent['url']=$url;
  43. $this->reqContent['data']=$data;
  44. }
  45. //获取结果内容
  46. function getResContent() {
  47. return $this->resContent;
  48. }
  49. //获取错误信息
  50. function getErrInfo() {
  51. return $this->errInfo;
  52. }
  53. //设置超时时间,单位秒
  54. function setTimeOut($timeOut) {
  55. $this->timeOut = $timeOut;
  56. }
  57. //执行http调用
  58. function call() {
  59. //启动一个CURL会话
  60. $ch = curl_init();
  61. // 设置curl允许执行的最长秒数
  62. curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeOut);
  63. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
  64. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
  65. // 获取的信息以文件流的形式返回,而不是直接输出。
  66. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  67. //发送一个常规的POST请求。
  68. curl_setopt($ch, CURLOPT_POST, 1);
  69. curl_setopt($ch, CURLOPT_URL, $this->reqContent['url']);
  70. //要传送的所有数据
  71. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->reqContent['data']);
  72. // 执行操作
  73. $res = curl_exec($ch);
  74. $this->responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  75. if ($res == NULL) {
  76. $this->errInfo = "call http err :" . curl_errno($ch) . " - " . curl_error($ch) ;
  77. curl_close($ch);
  78. return false;
  79. } else if($this->responseCode != "200") {
  80. $this->errInfo = "call http err httpcode=" . $this->responseCode ;
  81. curl_close($ch);
  82. return false;
  83. }
  84. curl_close($ch);
  85. $this->resContent = $res;
  86. return true;
  87. }
  88. function getResponseCode() {
  89. return $this->responseCode;
  90. }
  91. }
  92. ?>