博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
常用的php方法
阅读量:4494 次
发布时间:2019-06-08

本文共 2886 字,大约阅读时间需要 9 分钟。

/* * 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;}

不多说,有说明

转载于:https://www.cnblogs.com/wobeinianqing/p/6287236.html

你可能感兴趣的文章
lintcode 二叉树后序遍历
查看>>
vs c++ 转到声明失败
查看>>
Android实战技巧:如何在ScrollView中嵌套ListView
查看>>
Uva 1626 - Brakets Sequence(DP)
查看>>
makefile
查看>>
HTTP 错误 500.19 - Internal Server Error
查看>>
3.进制的转换
查看>>
python 装饰器
查看>>
从零开始编写自己的C#框架(26)——小结
查看>>
电子商务之性能测试
查看>>
MyBatis有哪些配置
查看>>
mongodb
查看>>
C# 依赖注入
查看>>
dede list 列表按文章权重排序
查看>>
前端实现app引导页面动画效果
查看>>
OpenGL的矩阵
查看>>
一天 zepto
查看>>
知乎趣闻
查看>>
centos使用--supervisor使用
查看>>
HDU5449 Robot Dog
查看>>