我编写了一个“proxy.php”脚本(如下所列),该脚本将获取?img =参数中指定的图像并将其打印到STDOUT。这是我的Flash应用程序在某些站点绕过丢失的crossdomain.xml所必需的。
它有效,但我有3个问题。我也是从Perl来到PHP并且在我的PHP知识方面仍然存在很多差距(但我意识到,stream_context_create和fpassthru可能使用了桶旅)。
1)在我的callback()函数中,如何将调试消息打印到PHP日志? (它被重定向到我的CentOS机器上的/ var / log / messages)
2)为什么我收到错误消息无法打开流:成功,我可能会错过回调中的案例()吗?
PHP Warning: fopen() [function.fopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/html/proxy.php on line 19
PHP Warning: fopen(http://i136.odnoklassniki.ru/getImage?photoId=154105499212&photoType=0) [function.fopen]: failed to open stream: Success in /var/www/html/proxy.php on line 19
3)因为我的脚本通常使用与参数相同的图像URL调用,所以我想扩展它以便在第一次调用时将获取的文件保存在目录中。在第一次和后续调用中,它应该将该缓存文件提供给STDOUT。你有什么建议,如何以保存记忆的方式制作它?即我不想用get_file_contents()一次读取整个文件
define('MAX_SIZE', 1024 * 1024);
$img = urldecode($_GET['img']);
if (strpos($img, '..') !== FALSE)
exit('Wrong URL: ' . $img);
$opts = array(
'http'=>array(
'method' => 'GET'
)
);
$ctx = stream_context_create($opts);
stream_context_set_params($ctx, array('notification' => 'callback'));
$fh = fopen($img, 'r', FALSE, $ctx);
if ($fh) {
fpassthru($fh);
fclose($fh);
}
function callback($code, $severity, $message, $message_code, $bytes_transferred, $bytes_total) {
if ($code == STREAM_NOTIFY_PROGRESS && $bytes_transferred > MAX_SIZE)
exit('File is too big: ' . $bytes_transferred);
if ($code == STREAM_NOTIFY_FILE_SIZE_IS)
if ($bytes_total > MAX_SIZE)
exit('File is too big: ' . $bytes_total);
else
header('Content-Length: ' . $bytes_total);
if ($code == STREAM_NOTIFY_MIME_TYPE_IS) {
if (stripos($message, 'image/gif') !== FALSE ||
stripos($message, 'image/png') !== FALSE ||
stripos($message, 'image/jpg') !== FALSE ||
stripos($message, 'image/jpeg') !== FALSE) {
header('Content-Type: ' . $message);
} else {
exit('File is not image: ' . $mime);
}
}
}
?>