|
- <?php
-
- /**
- * Created by PhpStorm.
- * User: Steven
- * Date: 2016/8/12
- * Time: 14:21
- */
- class app
- {
- public static $accountID = "f851a28e-3457-4bf7-810e-7b87307a8cdd";
- public static $accountPassword = "a401f1e8d973acbb1838b882caef3879";
- public static $url = "e.zhouzhuang.net/webservice/Order/Scenery.ashx";
- }
-
- class zzUtils
- {
- function create($array)
- {
- $xml = "";
- foreach ($array as $k => $v) {
- $xml .= "<" . $k . ">";
- if (is_array($v)) {
- $a = $this->create($v);
- $xml .= $a . "</" . $k . ">";
- } else {
- $xml .= $v . "</" . $k . ">";
- }
- }
- return $xml;
- }
-
- function array_to_xml($array)
- {
- $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
- $xml = $xml . $this->create($array);//递归
- return $xml;
-
- }
-
- //将xml转换为数组
- public function xml_to_array($xml)
- {
- $ob = simplexml_load_string($xml);
- $json = json_encode($ob);
- $array = json_decode($json, true);
- return $array;
- }
-
-
- public function xml_post_request($url, $xmlData)
- {
- $header[] = "Content-type: text/xml"; //定义content-type为xml,注意是数组
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);
- $response = curl_exec($ch);
- if (curl_errno($ch)) {
- print curl_error($ch);
- }
- curl_close($ch);
- return $response;
- }
-
- //发送HTTP请求
- public function httpsPost($url, $param = array())
- {
- $ch = curl_init(); // 初始化一个 cURL 对象
- curl_setopt($ch, CURLOPT_URL, $url); // 设置需要抓取的URL
- curl_setopt($ch, CURLOPT_HEADER, 0); // // 设置header
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
- // 如果你想PHP去做一个正规的HTTP POST,设置这个选项为一个非零值。这个POST是普通的 application/x-www-from-urlencoded 类型,多数被HTML表单使用。
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param)); // 传递一个作为HTTP “POST”操作的所有数据的字符串。//http_build_query:生成 URL-encode 之后的请求字符串
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-type:application/x-www-form-urlencoded;charset=utf-8'
- ));
- $rtn = curl_exec($ch); // 运行cURL,请求网页
- if ($errno = curl_errno($ch)) {
- throw new Exception ('Curl Error(' . $errno . '):' . curl_error($ch));
- }
- curl_close($ch); // 关闭URL请求
- return $rtn; // 返回获取的数据
- }
-
- public function getRequestBean()
- {
- $bean = simplexml_load_string(file_get_contents('php://input')); // simplexml_load_string() 函数把 XML 字符串载入对象中。如果失败,则返回 false。
- $request = array();
- foreach ($bean as $key => $value) {
- $request [( string )$key] = ( string )$value;
- }
- return $request;
- }
-
- //组装XML
- public function addXml($base_array, $array_header)
- {
- foreach ($array_header as $k => $v) {
- //return $base_array[$k] = $v;
- $base_array[$k] = $v;
- }
- return $base_array;
- }
-
- //生成数字签名sign
- function sign_md5($accountID, $accountPassword, $reqTime)
- {
- $data = $accountID . $accountPassword . $reqTime;
- return strtoupper(md5($data));
- }
-
- // 签名验证函数
- function ValidateSign($s1, $s2, $s3, $s4, $key, $sign)
- {
- $data = $s1 . $s2 . $s3 . $s4 . $key;
- if (md5($data) == $sign) {
- return true;
- } else {
- return false;
- }
- }
- }
|