常用的php方法

來源:互聯網
上載者:User

標籤:ram   top   init   strlen   cti   4.0   type   head   ret   

/* * 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(‘/\?[\d\D]+/‘,$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;}

不多說,有說明

常用的php方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.