get_headers函数模拟版

作者:matrix 发布时间:2014年9月27日 分类:零零星星

在sae上发现禁用了get_headers函数,只有另想办法,遂找到php 模拟get_headers函数代码,不过他的这个没有实现302跳转链接的跟踪。

这里自己的代码可以更高度模拟get_headers函数,利用php的curl功能

/*
模拟php的get_headers()函数;
在sae中需要关闭CURLOPT_FOLLOWLOCATION参数,否则不会有Location;缺点是没法跟踪跳转的链接
略有不同:Content-Length: 0 不会显示;一般的处理时没有问题的
*/
function getHeaders($url,$format=0,$FOLLOWLOCATION=1){
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    curl_setopt($curl, CURLOPT_NOBODY, 1);//不包含网页的内容
    if($FOLLOWLOCATION){
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);//允许链接自动跳转,当系统开启safe_mode或open_basedir,会出错,该关闭
    }
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1);//自动referer
    //curl_setopt($curl, CURLOPT_MAXREDIRS, 1);//限定CURLOPT_FOLLOWLOCATION递归返回的数量        
    $header = curl_exec($curl);
    curl_close($curl);
    $back =  array_filter(explode(PHP_EOL,$header));
    //return array_filter(explode(PHP_EOL,$header));
    if($format){
        foreach($back as $val)
        {
            if(preg_match('/^([^:]+): +(.*)$/',$val,$parts))
            {
                if (array_key_exists($parts[1],$v)){
                    $v[$parts[1]] = array_merge_recursive((array)$v[$parts[1]],(array)$parts[2]);
                }
                else
                {
                    $v[$parts[1]]=$parts[2];
                }
            }
            else{
                if(isset($v[0])){
                    $v[]=$val;
                }
                else{
                    $v[0]=$val;
                }
            }
        }
        return $v;
    }
    return $back;
}

说明:
getHeaders()函数的前两个参数和get_headers函数一样;
第三个参数:我在本地测试是没有问题的,只是在sae上测试不同,原因是sae的cul不支持CURLOPT_FOLLOWLOCATION参数,还有很多限制。这就添加个是否开启CURLOPT_FOLLOWLOCATION功能(自动跟踪跳转的链接);
本地测试基本上与get_headers函数相同输出,不影响响应头的获取。

参考:

http://www.phpernote.com/php-function/748.html

http://bbs.php100.com/read-htm-tid-148513.html

php的json_decode函数无法解析json

作者:matrix 发布时间:2014年9月4日 分类:零零星星

phpjson_decode函数用来解析json数据很方便,但是有时候却解析不了。

究其原因找到如下可能性:

1.键名没有用双引号括起来

['name':n,'age',a]
[name:n,age,a]

这两个都不能解析

2.出现多余逗号

['name':n,'age',a,]

###3.有些转义不支持
数据中出现\x26这样的会失败,有时候\'都无法解析。
stripslashes()去掉转义即可!

4.json不支持gbk编码

iconv('GBK', 'UTF-8', $json_data);//使用iconv()函数将GBK转到UTF-8编码

json数据解析前用检测工具测试一下较好:http://www.bejson.com/

150515添加

/* 
 格式化错误的json数据,使其能被json_decode()解析 
 不支持健名有中文、引号、花括号、冒号 
 不支持健指有冒号 
*/  
function format_ErrorJson($data,$quotes_key=false)  
{  
    $con = str_replace('\'','"',$data);//替换单引号为双引号 
    $con = str_replace(array('\\"'),array('<|YH|>'),$con);//替换 
    $con = preg_replace('/(\w+):[ {]?((?<YinHao>"?).*?\k<YinHao>[,}]?)/is', '"$1": $2',$con );//若键名没有双引号则添加  
    if($quotes_key)  
    {  
        $con = preg_replace('/("\w+"): ?([^"\s]+)([,}])[\s]?/is', '$1: "$2"$3',$con );//给键值添加双引号 
    } 
    $con = str_replace(array('<|YH|>'),array('\\"'),$con);//还原替换  
    return $con;  
}  

参考:http://bbs.csdn.net/topics/390496037

http://chenwei.me/p/59.html

Python版PHP内置的MD5()函数

作者:matrix 发布时间:2014年9月1日 分类:Python

初玩Python很不习惯那个md5函数。还好有人分享了相关代码,非常感谢。

import hashlib
def md5 (s, raw_output = False):
    res = hashlib.md5 (s)
    if raw_output:
        return res.digest ()
    return res.hexdigest ()

如果是python2.5 :

# Python 2.5+
import hashlib
hashlib.md5("welcome").hexdigest()
# pre-2.5, removed in Python 3
import md5
md5.md5("welcome").hexdigest()

参考:Python 实现PHP内置MD5函数方法