<?php
namespace Wechat\Lib;
use CURLFile;
/**
* 微信接口通用类
*
* @category WechatSDK
* @subpackage library
* @author Anyon <zoujingli@qq.com>
* @date 2016/05/28 11:55
*/
class Tools
{
static public function isBase64($str)
{
if ($str == base64_encode(base64_decode($str))) {
return true;
} else {
return false;
}
}
static public function createNoncestr($length = 32, $str = "")
{
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
static public function getSignature($data, $method = "sha1")
{
if (!function_exists($method)) {
return false;
}
ksort($data);
$params = array();
foreach ($data as $key => $value) {
$params[] = "{$key}={$value}";
}
return $method(join('&', $params));
}
static public function getPaySign($option, $partnerKey)
{
ksort($option);
$buff = '';
foreach ($option as $k => $v) {
$buff .= "{$k}={$v}&";
}
return strtoupper(md5("{$buff}key={$partnerKey}"));
}
static public function arr2xml($data, $root = 'xml', $item = 'item', $id = 'id')
{
return "<{$root}>" . self::_data_to_xml($data, $item, $id) . "</{$root}>";
}
static private function _data_to_xml($data, $item = 'item', $id = 'id', $content = '')
{
foreach ($data as $key => $val) {
is_numeric($key) && $key = "{$item} {$id}=\"{$key}\"";
$content .= "<{$key}>";
if (is_array($val) || is_object($val)) {
$content .= self::_data_to_xml($val);
} elseif (is_numeric($val)) {
$content .= $val;
} else {
$content .= '<![CDATA[' . preg_replace("/[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]/", '', $val) . ']]>';
}
list($_key, ) = explode(' ', $key . ' ');
$content .= "</$_key>";
}
return $content;
}
static public function xml2arr($xml)
{
$disableEntities = libxml_disable_entity_loader(true);
$result = json_decode(Tools::json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
libxml_disable_entity_loader($disableEntities);
return $result;
}
static public function json_encode($array)
{
return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', function ($matches) {
return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");
}, json_encode($array));
}
static public function httpGet($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
list($content, $status) = array(curl_exec($curl), curl_getinfo($curl), curl_close($curl));
return (intval($status["http_code"]) === 200) ? $content : false;
}
static public function httpPost($url, $data)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, self::_buildPost($data));
list($content, $status) = array(curl_exec($curl), curl_getinfo($curl), curl_close($curl));
return (intval($status["http_code"]) === 200) ? $content : false;
}
static public function httpsPost($url, $data, $ssl_cer = null, $ssl_key = null, $second = 30)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, $second);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if (!is_null($ssl_cer) && file_exists($ssl_cer) && is_file($ssl_cer)) {
curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($curl, CURLOPT_SSLCERT, $ssl_cer);
}
if (!is_null($ssl_key) && file_exists($ssl_key) && is_file($ssl_key)) {
curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($curl, CURLOPT_SSLKEY, $ssl_key);
}
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, self::_buildPost($data));
list($content, $status) = array(curl_exec($curl), curl_getinfo($curl), curl_close($curl));
return (intval($status["http_code"]) === 200) ? $content : false;
}
static private function _buildPost(&$data)
{
if (is_array($data)) {
foreach ($data as &$value) {
if (is_string($value) && $value[0] === '@' && class_exists('CURLFile', false)) {
$filename = realpath(trim($value, '@'));
file_exists($filename) && $value = new CURLFile($filename);
}
}
}
return $data;
}
static public function getAddress()
{
foreach (array('HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'HTTP_X_CLIENT_IP', 'HTTP_X_CLUSTER_CLIENT_IP', 'REMOTE_ADDR') as $header) {
if (!isset($_SERVER[$header]) || ($spoof = $_SERVER[$header]) === null) {
continue;
}
sscanf($spoof, '%[^,]', $spoof);
if (!filter_var($spoof, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$spoof = null;
} else {
return $spoof;
}
}
return '0.0.0.0';
}
static public function setCache($cachename, $value, $expired = 0)
{
return Cache::set($cachename, $value, $expired);
}
static public function getCache($cachename)
{
return Cache::get($cachename);
}
static public function removeCache($cachename)
{
return Cache::del($cachename);
}
static public function log($msg, $type = 'MSG')
{
Cache::put($type . ' - ' . $msg);
}
}