0

这是我在这里的第一个问题,所以很好......

我正在尝试使用html2canvas最终获取远程站点的屏幕截图(用户提交的 url)。

问题是跨域安全功能。我无法从画布上读取,因为它被场外资产锁定。

解决方案是使用库中内置的代理功能。

有一些带有python和 node.js 版本的 github 项目,但我需要在 php 中完成。

关于如何实现该功能以及如何使其工作的主题有很多,但没有一个真正解释如何制作自己的代理。

我的问题有两个方面,PHP 中有没有现有的解决方案?如果没有,我有几个关于自己制作的问题:1.)代理的输出格式是什么?json对象?渲染的图像?base64 编码的数据字符串?2.) 这些文件是否需要保留在服务器上,或者它们是否可以被渲染然后消失(覆盖)?

这大致是我的想法:

$img_url = urldecode($_GET['url']);
$img_data = base64_encode(file_get_contents($img_url));
//shouldn't need it since it's not cross domain now, but a CORS header could be inserted
header('content-type: application/json; charset=utf-8');
json_encode("{$_GET['callback']}($img_data)");
4

2 回答 2

0

我找到了我的问题的答案。代理功能接受带有代理图像 url 的 jsonp 元素。它们确实需要保存在服务器上,同时

这是原始的,我稍后会更新它,但这是一个适用于 html2canvas 的 PHP 代理脚本

session_start();
//parse the url sent by the proxy function
//TODO: scrub the input
$img_url = urldecode($_GET['url']);

//test file type
//TODO: test for other cases that don't have a '.'
$pos = strrpos($img_url, '.', -1);
$ext = substr($img_url, $pos);

//set a dir for this request
function randomNumber()
{
    return substr(sha1(rand()), 0, 15);
}

if (!isset($_COOKIE["img_path"]))
{
    do{
        $random = randomNumber();
    }while (is_dir('images/' . $random));

    setcookie("img_path", $random, time()+3600);

} else {
    $random = $_COOKIE["img_path"];
}

is_dir('images/' . $random) ? '' : mkdir('images/' . $random, 0755);

//TODO:catch cases where a filename isn't the last element
$basename = basename($img_url);

$file = 'images/' . $random . '/' .  $basename;

//save the image
copy($img_url, $file);

//TODO: don't hardcode the url
$test_location = "http://osc.test/html2canvas2/" . $file;

header('Content-Type: application/javascript');

echo  "{$_GET['callback']}(" . json_encode($test_location) . ")";
于 2013-01-24T01:41:57.073 回答
0

如果其他人正在寻找一个简单的 PHP 代理,这里有一个链接到一个不错的“牛仔”Ben Alman:

简单的 PHP 代理

于 2013-05-20T16:56:10.583 回答