PHP学习笔记

GET与POST请求中转

<?php
$protocol = ($_SERVER["HTTP_X_FORWARDED_PROTO"] == "https")?"https://":"http://";
$host = "www.baidu.com";
$content = file_get_contents('php://input');

$header_joins = array();
$headers = getAllHeaders();
$header_ignore = array('AppName','AppVersion','AccessKey','AppHash','MysqlPort','AppCookie','AppMask','AppSrvc','ipid');
foreach ($headers as $k => $v) {
    if(!in_array($k,$header_ignore))
        array_push($header_joins, $k . ': ' . $v);
}

function onHeaderReady($curlobj,$headerstr){
    header($headerstr);
    return strlen($headerstr);
}

function proxy_get($url, $headers) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_URL, $url);
    //curl_setopt($ch, CURLOPT_USERAGENT, '6666666666'); //模拟用户使用的浏览器
    @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1 );  // 使用自动跳转
    curl_setopt($ch, CURLOPT_TIMEOUT, 300);  //设置超时时间
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); // 收集结果而非直接展示
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // 自定义 Headers
    curl_setopt($ch, CURLOPT_HEADER,0);//输出header
    curl_setopt($ch, CURLOPT_HEADERFUNCTION,onHeaderReady);//header回调
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts  
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
    curl_exec($ch);
    curl_close($ch);
}

function proxy_post($url, $headers, $raw_data) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $raw_data);  // Post Data
    curl_setopt($ch, CURLOPT_URL, $url);//设置要访问的 URL
    //curl_setopt($ch, CURLOPT_USERAGENT, '6666666666'); //模拟用户使用的浏览器
    @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1 );  // 使用自动跳转
    curl_setopt($ch, CURLOPT_TIMEOUT, 300);  //设置超时时间
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); // 收集结果而非直接展示
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // 自定义 Headers
    curl_setopt($ch, CURLOPT_HEADER,0);//输出header
    curl_setopt($ch, CURLOPT_HEADERFUNCTION,onHeaderReady);//header回调
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts  
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
    curl_exec($ch);
    curl_close($ch);
}

if($_SERVER["REQUEST_METHOD"] == "GET"){
    proxy_get($protocol.$host.$_SERVER['REQUEST_URI'], $header_joins, $content);
}else if($_SERVER["REQUEST_METHOD"] == "POST"){
    proxy_post($protocol.$host.$_SERVER['REQUEST_URI'], $header_joins, $content);
}

?>

GET与POST

function curl_post($url='', $postdata='', $options=array()){
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    if (!empty($options)){
        curl_setopt_array($ch, $options);
    }
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
function curl_get($url='', $options=array()){
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    if (!empty($options)){
        curl_setopt_array($ch, $options);
    }
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
function curl_post_json($url='',$data=array(), $options=array())
{
    $data_string = json_encode($data);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string))
               );
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

###常用日期

echo date('Y-m-d');//2016-06-12
echo date('y-n-j');//16-6-12
echo date('l-w-N');//Sunday-0-7
//获取上个月第一天及最后一天.
echo date('Y-m-01', strtotime('-1 month'));
echo "<br/>";
echo date('Y-m-t', strtotime('-1 month'));
echo "<br/>";
//获取当月第一天及最后一天.
$BeginDate=date('Y-m-01', strtotime(date("Y-m-d")));
echo $BeginDate;
echo "<br/>";
echo date('Y-m-d', strtotime("$BeginDate +1 month -1 day"));
echo "<br/>";

###查看传入函数的参数

	$args = func_get_args();
	var_dump($args);

###对多维数组的某个键值排序

array_multisort(array1,sorting order,sorting type,array2,array3...)
/*
sorting order可选。规定排列顺序。可能的值:
SORT_ASC - 默认。按升序排列 (A-Z)。
SORT_DESC - 按降序排列 (Z-A)。
sorting type 可选。规定排序类型。可能的值:
SORT_REGULAR - 默认。把每一项按常规顺序排列(Standard ASCII,不改变类型)。
SORT_NUMERIC - 把每一项作为数字来处理。
SORT_STRING - 把每一项作为字符串来处理。
*/
array_multisort($ar1, $ar2);//将$ar2按$ar1排序
array_multisort($num,SORT_DESC,SORT_NUMERIC);

##数组中是否存在某值
in_array(value,array,type)
JQuery:
var arr = [ “xml”, “html”, “css”, “js” ];
$.inArray(“js”, arr); //返回 3,
如果不包含在数组中,则返回 -1;
##函数参数显性赋值

function foo3($array=array()) {
    extract($array);
    if (!isset($a)) $a=1;
    if (!isset($b)) $b=2;
    if (!isset($c)) $c=3;
    foo($a, $b, $c);
}
foo(array('a'=>1, 'c'=>3));

##把插入数据库中的id值取出来

$getID=mysql_insert_id();//返回在最后一次执行了 INSERT 查询后,由 AUTO_INCREMENT 定义的字段的值

##获取键名

array_keys(array) 

##时间毫秒

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

##判断时间段

date_default_timezone_set("Asia/Shanghai");
$time = intval (date("Hi"));
if ($time > "800" && $time < "1130") {
    // code
}

##网络

###POST

function httprequest($url,$post){
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, "$url"); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
 
    if($post){
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }
 
    $output = curl_exec($ch); 
    $httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
    curl_close($ch);
 
     if ($httpcode == 404) {
        return 404;
    }else{
        return $output;
    }
}

##背包动态规划

<?
header('Content-Type: text/html; charset=UTF-8');
$capacity=15;
$weight=array(3,4,5,6);
$choose=BBDTGH($capacity,$weight);
var_dump($weight);
echo '<br>';
var_dump($choose);
//背包动态规划
function BBDTGH($capacity,$weight)
{
	$count = count($weight);
	$value=$weight;
	$arr=array();
	for($i=0;$i<=$capacity;$i++){ $arr[0][$i]=0; }
	for($j=0;$j<=$count;$j++){ $arr[$j][0]=0; }
	for($j=1;$j<=$count;$j++)
	{
		for($i=1;$i<=$capacity;$i++)
		{
			$arr[$j][$i]=$arr[$j-1][$i];
			if($weight[$j-1]<=$capacity)
			{
				if(!isset($arr[$j-1][$i-$weight[$j-1]]))
					continue;
				$tmp = $arr[$j-1][$i-$weight[$j-1]]+$value[$j-1];
				if($tmp>$arr[$j][$i])
				{
					$arr[$j][$i]=$tmp;
				}
			}
		}
	}
	$j=$capacity;
	$res=array_fill(0,$count,0);
	for ($i=$count;$i>=1;$i--) {
		if($arr[$i][$j]>$arr[$i-1][$j])
		{
			$res[$i-1]=1;
			$j = $j - $weight[$i-1];
		}
	}
	return $res;
}
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值