常用的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方法的更多相关文章
- Node.js process 模块常用属性和方法
Node.js是常用的Javascript运行环境,本文和大家发分享的主要是Node.js中process 模块的常用属性和方法,希望通过本文的分享,对大家学习Node.js http://www.m ...
- Java常用的输入输出方法
对于经常上机刷题的来说,首先得解决输入输出方法,Java的输入输出流在Java学习过程的后面部分才会接触,但是我们可以掌握一些简单的,常用的输入输出方法 首先输出 大家最熟悉的莫过于输出方法,直接用S ...
- JavaScript常用对象的方法和属性
---恢复内容开始--- 本文将简单介绍JavaScript中一些常用对象的属性和方法,以及几个有用的系统函数. 一.串方法 JavaScript有强大的串处理功能,有了这些串方法,才能编写出丰富多彩 ...
- Document-对象属性和常用的对象方法
Document-对象属性和常用的对象方法 对象属性 document.title //设置文档标题等价于HTML的title标签 document ...
- 干货:结合Scikit-learn介绍几种常用的特征选择方法
原文 http://dataunion.org/14072.html 主题 特征选择 scikit-learn 作者: Edwin Jarvis 特征选择(排序)对于数据科学家.机器学习从业者来说非 ...
- ios基础篇(四)——UILabel的常用属性及方法
UILabel的常用属性及方法:1.text //设置和读取文本内容,默认为nil label.text = @”文本信息”; //设置内容 NSLog(@”%@”, label.text); //读 ...
- (转)在网页中JS函数自动执行常用三种方法
原文:http://blog.sina.com.cn/s/blog_6f6b4c3c0100nxx8.html 在网页中JS函数自动执行常用三种方法 在网页中JS函数自动执行常用三种方法 在HTML中 ...
- 结合Scikit-learn介绍几种常用的特征选择方法
特征选择(排序)对于数据科学家.机器学习从业者来说非常重要.好的特征选择能够提升模型的性能,更能帮助我们理解数据的特点.底层结构,这对进一步改善模型.算法都有着重要作用. 特征选择主要有两个功能: 减 ...
- sql常用的星期方法
sql常用的星期方法: SELECT convert(varchar(10),DATEADD(wk, DATEDIFF(wk,0,getdate()), 0),120) --本周开始周一SELECT ...
- jquery常用函数与方法汇总
1.delay(duration,[queueName]) 设置一个延时来推迟执行队列中之后的项目. jQuery1.4新增.用于将队列中的函数延时执行.他既可以推迟动画队列的执行,也可以用于自定义队 ...
随机推荐
- Python中的常见方法
Python中有三种比较常见的方法类型,如类方法和静态方法,实例方法,他们是面向对象编程中重要的概念. 1.类方法 类方法是通过使用装饰器@classmethod来定义的,他的第一个参数是cls,指向 ...
- Java 代码实现POST/GET请求
方式一 package com.cyb.util; import java.io.BufferedReader; import java.io.DataOutputStream; import jav ...
- SpringMVC springmvc.xml配置路径前缀和后缀
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...
- 使用requests库实现http请求
1.发送请求 import requests url = 'http://www.tipdm.com/tipdm/index.html' rqq = requests.get(url) In [ ]: ...
- 用ssh同时push 项目到github和gitee的方法
分别为两个网站声称pubkey cd ./ssh ssh-keygen -t rsa -C "oeasy@oeasy.org" -f "github_id_rsa&q ...
- 题解:AT_abc359_e [ABC359E] Water Tank
背景 中考结束了,但是暑假只有一天,这就是我现在能在机房里面写题解的原因-- 分析 这道题就是个单调栈. 题目上问你第一滴水流到每个位置的时间.我们考虑,答案其实就是比当前木板高且距离当前木板最近的那 ...
- 阅读翻译Mathematics for Machine Learning之2.6 Generating Set and Basis
阅读翻译Mathematics for Machine Learning之2.6 Generating Set and Basis 关于: 首次发表日期:2024-07-19 Mathematics ...
- Django--StreamingHttpResponse下载文件
from django.shortcuts import render, HttpResponse from django.http import StreamingHttpResponse impo ...
- vue 拖拉改变盒子高度(mousedown、mousemove、mouseup)流畅不卡顿
需求:上下两个盒子之间添加可拖拽按钮,实现高度变化 html: <textarea :id="'mycode'+(index*1+1)" :ref="'mycode ...
- 服务端渲染中的数据获取:结合 useRequestHeaders 与 useFetch
title: 服务端渲染中的数据获取:结合 useRequestHeaders 与 useFetch date: 2024/7/24 updated: 2024/7/24 author: cmdrag ...