sina微博api源码阅读之函数

1. array_map将类的静态成员函数作为回调函数用在指定的单元上,可以递归调用

public static function urlencode_rfc3986($input) {
        if (is_array($input)) {
            return array_map(array('OAuthUtil', 'urlencode_rfc3986'), $input);
        } else if (is_scalar($input)) {
            return str_replace(
                '+',
                ' ',
                str_replace('%7E', '~', rawurlencode($input))
            );
        } else {
            return '';
        }
    }

关于类内静态回调:

写成array_map('OAuthUtil::urlencode_rfc3986', $input);

or array_map(array(__CLASS__, 'urlencode_rfc3986'), $input);

or array(__CLASS__, __METHOD__) 都是可以的,但是方法必须是static方法。

__CLASS__, __METHOD__是当前类名和方法名的魔术常量,同__line__。

在这里用$this or self都是不可以的。可以写foreach循环,然后用self::urlencode_rfc3986方法。

如果是非static成员变量,array_map(array($this,'urltranscode'), $value);

$this是对象的引用,实例化时确定的。

self指向类本身,与实例化对象无关,用来调用一些静态变量和函数。

 

2. php输入流

参照http://blog.haohtml.com/index.php/archives/5125

php://input可以读取没有处理过的POST数据。相较于$HTTP_RAW_POST_DATA而言,它给内存带来的压力较小,并且不需要特殊的php.ini设置。php://input不能用于enctype=multipart/form-data

<?php
//@file phpinput_post.php
$http_entity_body = 'n=' . urldecode('perfgeeks') . '&amp;p=' . urldecode('7788');
$http_entity_type = 'application/x-www-form-urlencoded';
$http_entity_length = strlen($http_entity_body);
$host = '192.168.0.6';
$port = 80;
$path = '/phpinput_server.php';
$fp = fsockopen($host, $port, $error_no, $error_desc, 30);
if ($fp) {
fputs($fp, "POST {$path} HTTP/1.1\r\n");
fputs($fp, "Host: {$host}\r\n");
fputs($fp, "Content-Type: {$http_entity_type}\r\n");
fputs($fp, "Content-Length: {$http_entity_length}\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $http_entity_body . "\r\n\r\n");

while (!feof($fp)) {
$d .= fgets($fp, 4096);
}
fclose($fp);
echo $d;
}
?>
在server端,可以通过一下函数获取post的数据
$raw_post_data = file_get_contents('php://input', 'r');
1,Coentent-Type仅在取值为application/x-www-data-urlencoded和multipart/form-data两种情况下,PHP才会将http请求数据包中相应的数据填入全局变量$_POST
2,PHP不能识别的Content-Type类型的时候,会将http请求包中相应的数据填入变量$HTTP_RAW_POST_DATA
3,  只有Content-Type不为multipart/form-data的时候,PHP不会将http请求数据包中的相应数据填入php://input,否则其它情况都会。填入的长度,由Coentent-Length指定。
4,只有Content-Type为application/x-www-data-urlencoded时,php://input数据才跟$_POST数据相一致。
5,php://input数据总是跟$HTTP_RAW_POST_DATA相同,但是php://input比$HTTP_RAW_POST_DATA更凑效,且不需要特殊设置php.ini
6,PHP会将PATH字段的query_path部分,填入全局变量$_GET。通常情况下,GET方法提交的http请求,body为空。

posted @ 2011-01-18 10:44  hayley  阅读(415)  评论(0编辑  收藏  举报