$v) { $xml .= "<" . $k . ">"; if (is_array($v)) { $a = self::create($v); $xml .= $a . ""; } else { $xml .= $v . ""; } } return $xml; } static function array_to_xml($array) { $xml = ""; $xml = $xml . self::create($array);//递归 return $xml; } //发送get参数(链接,参数数组) static function send_get($url, $get_data) { $url_param = http_build_query($get_data); $result = file_get_contents($url . "?{$url_param}"); return $result; } //将xml转换为数组 static function xml_to_array($xml) { $ob = simplexml_load_string($xml); $json = json_encode($ob); $array = json_decode($json, true); return $array; } //发送post请求(xml) static 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; } /** * PHP发送Json对象数据 * * @param $url 请求url * @param $jsonStr 发送的json字符串 * @return array */ static function http_post_json($url, $jsonStr) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($jsonStr) ) ); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); return array($httpCode, $response); } /** * PHP发送xml数据 * * @param $url 请求url * @param $xmlStr 发送的xml * @return array */ static function http_post_xml($url, $xmlStr) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlStr); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8')); $response = curl_exec($ch); if ($errno = curl_errno($ch)) { throw new Exception ('Curl Error(' . $errno . '):' . curl_error($ch)); } curl_close($ch); // 关闭URL请求 return $response; // 返回获取的数据 } /** * PHP发送数组 * * @param $url 请求url * @param $param 发送的数组 * @return array */ static 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; // 返回获取的数据 } static function base_xml_encode($data, $encoding = 'utf-8', $root = "root") { $xml = "\n"; $xml .= "<$root>"; $xml .= self::data_to_xml($data); $xml .= ""; return $xml; } //接受xml数据并转化成数组 static 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; } //接受json数据并转化成数组 static function getJsonData() { $bean = file_get_contents('php://input'); $result = json_decode($bean, true); return $result; } //组装XML static function data_to_xml($data) { if (is_object($data)) { $data = get_object_vars($data); } $xml = ''; foreach ($data as $key => $val) { if (is_null($val)) { $xml .= "<$key/>\n"; } else { is_numeric($key) && $key = "item"; $xml .= "<$key>"; $xml .= (is_array($val) || is_object($val)) ? self::data_to_xml($val) : $val; list($key,) = explode(' ', $key); $xml .= ""; } } return $xml; } /** * 日志方法 * @param $log */ static function writeLog($log) { $dir = __DIR__ . "/../Log/"; if (!is_dir($dir)) { mkdir($dir); } $filename = $dir . date("Y-m-d") . ".log"; file_put_contents($filename, date("Y-m-d H:i:s") . "\t" . $log . PHP_EOL, FILE_APPEND); } /** * 签名验证函数 * @param $param 需要加密的字符串 * @param $sign 第三方已经机密好的用来比对的字串 * @return bool */ static function ValidateSign($param, $sign) { if (md5($param) == $sign) { return true; } else { return false; } } }