PHP常用功能组件方法---代码块

匿名 (未验证) 提交于 2019-12-02 22:11:45

工作中常用的几个功能封装,收集整理,留用2018年6月23日15:43:03

xml 转 array数组

   /**     * xml转为array     * @param string $xml     * @throws WxPayException     */ public function FromXml($xml) {      if(!$xml){       throw new Exception("xml");    }        //XML转为array        //xml       libxml_disable_entity_loader(true);        $this->values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);           return $this->values; } 

对象中数组 转 xml

/**  * xml * @throws WxPayException **/ public function ToXml() {    if(!is_array($this->values)        || count($this->values) <= 0)    {           throw new Exception("");        }                $xml = "<xml>";        foreach ($this->values as $key=>$val)        {           if (is_numeric($val)){              $xml.="<".$key.">".$val."</".$key.">";           }else{              $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";           }        }        $xml.="</xml>";        return $xml;  }

格式化URL参数

/**  * url */ public function ToUrlParams() {    $buff = "";    foreach ($this->values as $k => $v)    {       if($k != "sign" && $v != "" && !is_array($v)){          $buff .= $k . "=" . $v . "&";       }    }        $buff = trim($buff, "&");    return $buff; }

生成32位随机数

/**  *   * 32 * @param int $length  * @return  */ public static function getNonceStr($length = 32)  {    $chars = "abcdefghijklmnopqrstuvwxyz0123456789";      $str ="";    for ( $i = 0; $i < $length; $i++ )  {         $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);      }     return $str; }

curl方法post请求xml提交

/**  * postxmlurl  *   * @param string $xml  postxml * @param string $url  url  * @param bool $useCert  * @param int $second   url30s  * @throws WxPayException  */ private static function postXmlCurl($xml, $url, $useCert = false, $second = 30) {         $ch = curl_init();    //   curl_setopt($ch, CURLOPT_TIMEOUT, $second);        //   if(WxPayConfig::CURL_PROXY_HOST != "0.0.0.0"        && WxPayConfig::CURL_PROXY_PORT != 0){       curl_setopt($ch,CURLOPT_PROXY, WxPayConfig::CURL_PROXY_HOST);       curl_setopt($ch,CURLOPT_PROXYPORT, WxPayConfig::CURL_PROXY_PORT);    }    curl_setopt($ch,CURLOPT_URL, $url);    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE); //true    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, 2);// 02,2    //璁剧疆header    curl_setopt($ch, CURLOPT_HEADER, FALSE);    //   curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);     if($useCert == true){       //      //cert  key .pem      curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');       curl_setopt($ch,CURLOPT_SSLCERT, WxPayConfig::SSLCERT_PATH);       curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');       curl_setopt($ch,CURLOPT_SSLKEY, WxPayConfig::SSLKEY_PATH);    }    //post   curl_setopt($ch, CURLOPT_POST, TRUE);    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);    //curl    $data = curl_exec($ch);    //   if($data){       curl_close($ch);       return $data;    } else {        $error = curl_errno($ch);       curl_close($ch);       throw new WxPayException("curl:$error");    } } 

获取毫秒时间戳

 /**  *  */ private static function getMillisecond() {    //   $time = explode ( " ", microtime () );    $time = $time[1] . ($time[0] * 1000);    $time2 = explode( ".", $time );    $time = $time2[0];    return $time; }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!