Zend_Uri_Http:使用 Google Chart URI 时提供的 URI 无效?

发布于 2024-09-14 17:33:04 字数 1639 浏览 9 评论 0原文

我的应用程序使用 Google Charts 并使用 HTTPS。我需要将 Google 图表显示为“安全”图像,否则 Internet Explorer 会抱怨显示不安全的内容。因此,我尝试使用 Zend_Http_Client 请求下载它们(然后链接到本地​​文件),但我似乎无法做到这一点。

该 URI 应该有效,因为我可以单击此链接并查看图像:

http://chart.apis.google .com/chart?chs=250x150&cht=bvg&chds=0,19&chd=t:19&chxt=x,y&chxl=0:|2009&chxr=1,0,19&chf=bg, s,F2F0E1

这是我正在使用的代码:

$chartUrl = 'http://chart.apis.google.com/chart?chs=250x150&cht=bvg&chds=0,19&chd=t:19&chxt=x,y&chxl=0:|2009&chxr=1,0,19&chf=bg,s,F2F0E1';
$client = new Zend_Http_Client($chartUrl, array('maxredirects' => 0, 'timeout' => 30));
$response = $client->request();

我做错了什么?我还有其他方法可以实现这一目标吗?

解决方法

由于 Google Chart URI 使用“无效”字符,因此在构造 Zend_Uri 时验证失败。这就是我下载谷歌图表所必须要做的。

/**
 * Returns the URL of the Google chart image that has been downloaded and stored locally. If it has not been downloaded yet, it will be download.
 * @param string $chartUrl
 * @return string
 */
protected function _getLocalImageUrl($chartUrl)
{
    $savePath = realpath(APPLICATION_PATH . '/../public/Resources/google-charts/');
    $hashedChartUrl = md5($chartUrl);
    $localPath = "$savePath/$hashedChartUrl";
    
    if (!file_exists($localPath)) {
        exec("wget -O \"$localPath\" \"$chartUrl\"");
    }
    
    return "/Resources/google-charts/$hashedChartUrl";
}

My application uses Google Charts and using HTTPS. I need to display the Google Charts as "secure" images, otherwise Internet Explorer will complain about displaying insecure content. So, I am trying to download them (then link to the local file) using a Zend_Http_Client request, but I can't seem to do it.

The URI should be valid since I can click this link and view the image:

http://chart.apis.google.com/chart?chs=250x150&cht=bvg&chds=0,19&chd=t:19&chxt=x,y&chxl=0:|2009&chxr=1,0,19&chf=bg,s,F2F0E1

Here's the code I am using:

$chartUrl = 'http://chart.apis.google.com/chart?chs=250x150&cht=bvg&chds=0,19&chd=t:19&chxt=x,y&chxl=0:|2009&chxr=1,0,19&chf=bg,s,F2F0E1';
$client = new Zend_Http_Client($chartUrl, array('maxredirects' => 0, 'timeout' => 30));
$response = $client->request();

What am I doing wrong? Is there another way I can achieve this?

Work-around

Since the Google Chart URI uses "invalid" characters, it fails validation when constructing a Zend_Uri. This is what I had to do in order to download the Google Chart.

/**
 * Returns the URL of the Google chart image that has been downloaded and stored locally. If it has not been downloaded yet, it will be download.
 * @param string $chartUrl
 * @return string
 */
protected function _getLocalImageUrl($chartUrl)
{
    $savePath = realpath(APPLICATION_PATH . '/../public/Resources/google-charts/');
    $hashedChartUrl = md5($chartUrl);
    $localPath = "$savePath/$hashedChartUrl";
    
    if (!file_exists($localPath)) {
        exec("wget -O \"$localPath\" \"$chartUrl\"");
    }
    
    return "/Resources/google-charts/$hashedChartUrl";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

审判长 2024-09-21 17:33:04

尝试:$chartUrl = 'http://chart.apis.google.com/chart?chs=250x150&cht=bvg&chds=0,19&chd=t%3A19&chxt=x,y&chxl =0%3A|2009&chxr=1,0,19&chf=bg,s,F2F0E1';

[编辑]
正如评论中提到的,urlencode 没有解决问题,但用十六进制值替换冒号可以解决问题。

Try: $chartUrl = 'http://chart.apis.google.com/chart?chs=250x150&cht=bvg&chds=0,19&chd=t%3A19&chxt=x,y&chxl=0%3A|2009&chxr=1,0,19&chf=bg,s,F2F0E1';

[edit]
As mentioned in the comments, urlencode didn't fix the issue, but replacing colons with their hex value did.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文