zoukankan      html  css  js  c++  java
  • 常用的php方法

    /*
     * http 封装网络请求方法
     */
     /*
     * get method
     */
     function get($url, $param=array()){
        if(!is_array($param)){
            throw new Exception("参数必须为array");
        }
        $p='';
        foreach($param as $key => $value){
            $p=$p.$key.'='.$value.'&';
        }
        if(preg_match('/?[dD]+/',$url)){//matched ?c
            $p='&'.$p;
        }else if(preg_match('/?$/',$url)){//matched ?$
            $p=$p;
        }else{
            $p='?'.$p;
        }
        $p=preg_replace('/&$/','',$p);
        $url=$url.$p;
        //echo $url;
        $httph =curl_init($url);
        curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($httph,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
        
        curl_setopt($httph, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($httph, CURLOPT_HEADER,1);
        $rst=curl_exec($httph);
        curl_close($httph);
        return $rst;
     }
     /*
     * post method
     */
     function post($url, $param=array()){
        if(!is_array($param)){
            throw new Exception("参数必须为array");
        }
        $httph =curl_init($url);
        curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($httph,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
        curl_setopt($httph, CURLOPT_POST, 1);//设置为POST方式 
        curl_setopt($httph, CURLOPT_POSTFIELDS, $param);
        curl_setopt($httph, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($httph, CURLOPT_HEADER,1);
        $rst=curl_exec($httph);
        curl_close($httph);
        return $rst;
     }
     
     /**
     * PHP发送Json对象数据
     *
     * @param $url 请求url
     * @param $jsonStr 发送的json字符串
     * @return array
     */
     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 获取目录树 
      * 
      * @param  [string] $path [目录路径]
      * @return [array]       [目录结构数组]
      */
     function dirtree($path) {
        $handle = opendir($path);
        $itemArray=array();
        while (false !== ($file = readdir($handle))) {
            if (($file=='.')||($file=='..')){
                
            }elseif (is_dir($path.$file)) {
                    try {
                        $dirtmparr=dirtree($path.$file.'/');
                    } catch (Exception $e) {
                        $dirtmparr=null;
                    };
                    $itemArray[$file]=$dirtmparr;
                }else{
                    array_push($itemArray, $file);
                }
            }
        return $itemArray;
    }

    不多说,有说明

  • 相关阅读:
    通俗算法教程04
    微软是如何重写C#编译器并使它开源的
    在 CentOS 7 中安装 MySQL 8
    在 .NET Core 中结合 HttpClientFactory 使用 Polly(下篇)
    在 .NET Core 中结合 HttpClientFactory 使用 Polly(中篇)
    .NET 开源项目 Polly 介绍
    在 .NET Core 中结合 HttpClientFactory 使用 Polly(上篇)
    5年后、10年后,你希望自己是个什么样的人?
    即将发布的 ASP.NET Core 2.2 会有哪些新玩意儿?
    推荐六本前端开发必看的书籍
  • 原文地址:https://www.cnblogs.com/wobeinianqing/p/6287236.html
Copyright © 2011-2022 走看看