PayUtils.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace common\pay\dinpay;
  3. class PayUtils
  4. {
  5. /**
  6. * 发送post请求
  7. * @param string $url 请求地址
  8. * @param array $post_data post键值对数据
  9. * @return string
  10. */
  11. public static function sendPost($url, $post_data) {
  12. $postdata = http_build_query($post_data);
  13. $options = array(
  14. 'http' => array(
  15. 'method' => 'POST',
  16. 'header' => 'Content-type:application/x-www-form-urlencoded',
  17. 'content' => $postdata,
  18. 'timeout' => 15 * 60 // 超时时间(单位:s)
  19. )
  20. );
  21. $context = stream_context_create($options);
  22. $result = file_get_contents($url, false, $context);
  23. return $result;
  24. }
  25. /**
  26. * 构造自动提交表单
  27. * @param array $params
  28. * @param string $action
  29. * @return string
  30. */
  31. public static function createHtml($params, $action)
  32. {
  33. $encodeType = isset ($params ['encoding']) ? $params ['encoding'] : 'UTF-8';
  34. $html = <<<eot
  35. <html>
  36. <head>
  37. <meta http-equiv="Content-Type" content="text/html; charset={$encodeType}" />
  38. </head>
  39. <body onload="javascript:document.pay_form.submit();">
  40. <form id="pay_form" name="pay_form" action="{$action}" method="post">
  41. eot;
  42. foreach ($params as $key => $value) {
  43. $html .= " <input type=\"hidden\" name=\"{$key}\" id=\"{$key}\" value=\"{$value}\" />\n";
  44. }
  45. $html .= <<<eot
  46. <!-- <input type="submit" type="hidden">-->
  47. </form>
  48. </body>
  49. </html>
  50. eot;
  51. return $html;
  52. }
  53. }