常用的php方法
Posted 我辈年轻
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了常用的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方法的主要内容,如果未能解决你的问题,请参考以下文章