AdaRequests.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace AdaPay;
  3. class AdaRequests {
  4. public $postCharset = "utf-8";
  5. public function curl_request($url, $postFields = null, $headers=null, $is_json=false) {
  6. AdaPay::writeLog("curl方法参数:". json_encode(func_get_args(), JSON_UNESCAPED_UNICODE), "INFO");
  7. $ch = curl_init();
  8. curl_setopt($ch, CURLOPT_URL, $url);
  9. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  10. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  11. curl_setopt($ch, CURLOPT_HEADER, 0);
  12. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  13. if (is_array($postFields) && 0 < count($postFields)) {
  14. curl_setopt($ch, CURLOPT_POST, true);
  15. if ($is_json) {
  16. $json_data = json_encode($postFields);
  17. AdaPay::writeLog("post-json请求参数:". json_encode($postFields, JSON_UNESCAPED_UNICODE), "INFO");
  18. array_push($headers, "Content-Length:". strlen($json_data));
  19. curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
  20. }else{
  21. AdaPay::writeLog("post-form请求参数:". json_encode($postFields, JSON_UNESCAPED_UNICODE), "INFO");
  22. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  23. }
  24. }
  25. if (empty($headers)){
  26. $headers = array('Content-type: application/x-www-form-urlencoded');
  27. }
  28. AdaPay::writeLog("curl请求头:". json_encode($headers, JSON_UNESCAPED_UNICODE), "INFO");
  29. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  30. $response = curl_exec($ch);
  31. $statuCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
  32. if (curl_errno($ch)) {
  33. AdaPay::writeLog(curl_error($ch), "ERROR");
  34. }
  35. curl_close($ch);
  36. AdaPay::writeLog("curl返回参数:". $statuCode. json_encode($response, JSON_UNESCAPED_UNICODE), "INFO");
  37. return array($statuCode, $response);
  38. }
  39. function characet($data, $targetCharset) {
  40. if (!empty($data)) {
  41. $fileType = $this->postCharset;
  42. if (strcasecmp($fileType, $targetCharset) != 0) {
  43. $data = mb_convert_encoding($data, $targetCharset, $fileType);
  44. }
  45. }
  46. return $data;
  47. }
  48. }