首页 > 代码库 > 常用的php方法
常用的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(‘/\?[\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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。