選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

128 行
4.1 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Steven
  5. * Date: 2016/8/12
  6. * Time: 14:21
  7. */
  8. class app
  9. {
  10. public static $accountID = "f851a28e-3457-4bf7-810e-7b87307a8cdd";
  11. public static $accountPassword = "a401f1e8d973acbb1838b882caef3879";
  12. public static $url = "e.zhouzhuang.net/webservice/Order/Scenery.ashx";
  13. }
  14. class zzUtils
  15. {
  16. function create($array)
  17. {
  18. $xml = "";
  19. foreach ($array as $k => $v) {
  20. $xml .= "<" . $k . ">";
  21. if (is_array($v)) {
  22. $a = $this->create($v);
  23. $xml .= $a . "</" . $k . ">";
  24. } else {
  25. $xml .= $v . "</" . $k . ">";
  26. }
  27. }
  28. return $xml;
  29. }
  30. function array_to_xml($array)
  31. {
  32. $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  33. $xml = $xml . $this->create($array);//递归
  34. return $xml;
  35. }
  36. //将xml转换为数组
  37. public function xml_to_array($xml)
  38. {
  39. $ob = simplexml_load_string($xml);
  40. $json = json_encode($ob);
  41. $array = json_decode($json, true);
  42. return $array;
  43. }
  44. public function xml_post_request($url, $xmlData)
  45. {
  46. $header[] = "Content-type: text/xml"; //定义content-type为xml,注意是数组
  47. $ch = curl_init($url);
  48. curl_setopt($ch, CURLOPT_URL, $url);
  49. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  50. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  51. curl_setopt($ch, CURLOPT_POST, 1);
  52. curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);
  53. $response = curl_exec($ch);
  54. if (curl_errno($ch)) {
  55. print curl_error($ch);
  56. }
  57. curl_close($ch);
  58. return $response;
  59. }
  60. //发送HTTP请求
  61. public function httpsPost($url, $param = array())
  62. {
  63. $ch = curl_init(); // 初始化一个 cURL 对象
  64. curl_setopt($ch, CURLOPT_URL, $url); // 设置需要抓取的URL
  65. curl_setopt($ch, CURLOPT_HEADER, 0); // // 设置header
  66. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
  67. // 如果你想PHP去做一个正规的HTTP POST,设置这个选项为一个非零值。这个POST是普通的 application/x-www-from-urlencoded 类型,多数被HTML表单使用。
  68. curl_setopt($ch, CURLOPT_POST, 1);
  69. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param)); // 传递一个作为HTTP “POST”操作的所有数据的字符串。//http_build_query:生成 URL-encode 之后的请求字符串
  70. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  71. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  72. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  73. 'Content-type:application/x-www-form-urlencoded;charset=utf-8'
  74. ));
  75. $rtn = curl_exec($ch); // 运行cURL,请求网页
  76. if ($errno = curl_errno($ch)) {
  77. throw new Exception ('Curl Error(' . $errno . '):' . curl_error($ch));
  78. }
  79. curl_close($ch); // 关闭URL请求
  80. return $rtn; // 返回获取的数据
  81. }
  82. public function getRequestBean()
  83. {
  84. $bean = simplexml_load_string(file_get_contents('php://input')); // simplexml_load_string() 函数把 XML 字符串载入对象中。如果失败,则返回 false。
  85. $request = array();
  86. foreach ($bean as $key => $value) {
  87. $request [( string )$key] = ( string )$value;
  88. }
  89. return $request;
  90. }
  91. //组装XML
  92. public function addXml($base_array, $array_header)
  93. {
  94. foreach ($array_header as $k => $v) {
  95. //return $base_array[$k] = $v;
  96. $base_array[$k] = $v;
  97. }
  98. return $base_array;
  99. }
  100. //生成数字签名sign
  101. function sign_md5($accountID, $accountPassword, $reqTime)
  102. {
  103. $data = $accountID . $accountPassword . $reqTime;
  104. return strtoupper(md5($data));
  105. }
  106. // 签名验证函数
  107. function ValidateSign($s1, $s2, $s3, $s4, $key, $sign)
  108. {
  109. $data = $s1 . $s2 . $s3 . $s4 . $key;
  110. if (md5($data) == $sign) {
  111. return true;
  112. } else {
  113. return false;
  114. }
  115. }
  116. }