示例代码:
<?php
set_time_limit(0);
$authorization = ""; // OAuth authorization credentials
$fp = fsockopen("ssl://userstream.twitter.com", 443);
$headers = array(
"GET /1.1/user HTTP/1.1",
"Host: userstream.twitter.com",
"Accept-Encoding: deflate, gzip",
"Authorization: OAuth {$authorization}",
"",
"",
);
fwrite($fp, implode("\r\n", $headers));
while (!feof($fp)) {
if (!$size = hexdec(fgets($fp))) {
break;
}
echo DECODE_FUNCTION(fread($fp, $size));
fgets($fp); // SKIP CRLF
}
如果我将DECODE_FUNCTION实现为:
,则此示例有效function DECODE_FUNCTION($str) {
$filename = stream_get_meta_data($fp = tmpfile())['uri'];
fwrite($fp, $str);
ob_start();
readgzfile($filename);
return ob_get_clean();
}
然而,这些案件失败了:
function DECODE_FUNCTION($str) {
return gzuncompress($str);
}
或
function DECODE_FUNCTION($str) {
return gzinflate($str);
}
或
function DECODE_FUNCTION($str) {
return gzdecode($str);
}
创建临时文件似乎有很多开销。 什么是最好的方式?
谢谢。