0

我有一个网络服务,应该为我的网站提供图像。问题是图像受“令牌”请求标头保护。

我使用 guzzle 作为客户端库来调用 web 服务并获取图像。

$client = new \Guzzle\Service\Client();
$req = $client->get('https://www.customwebservice.be/v1/api/photo/ece1d868.jpg');
$req->addHeader('access_tokenid', 'token123');
$response = $req->send();
$test = ($response->getInfo());
echo '<img src="' . $test['url'] . '">';

但没有结果:S

如果我设置了一个与 web 服务具有相同域的 cookie,并且它可以冲浪到 url,但我不能为每个用户设置一个令牌 cookie,对吗?

提前谢谢

4

1 回答 1

0

我不知道向用户隐藏此请求令牌有多重要,但如果是这种情况,您可以让您的应用程序充当获取/提供图像的代理。

为此,您可以添加一个自定义路由/操作,直接将图像传递给您的客户端,而不需要临时存储它。例子:

public function imageAction($filename)
{
    $response = \GuzzleHttp\get(
        'https://www.customwebservice.be/v1/api/photo/' . $filename,
        array('headers' => array('access_tokenid' => 'token123'))
    );

    return new Response(
        $response->getBody(),
        200,
        array('content-type' => $response->getContentType())
    );
}

您的模板将如下所示:

<img src="{{ path('image', {'filename': 'ece1d868.jpg'}) }}" />

请注意,这是一个性能相当低的解决方案,因此如果您的应用程序服务于大量图像密集的页面或必须处理大量流量,您应该考虑实施某种缓存解决方案。

于 2014-03-19T21:23:29.310 回答