Zend_Uri_Http:使用 Google Chart URI 时提供的 URI 无效?
我的应用程序使用 Google Charts 并使用 HTTPS。我需要将 Google 图表显示为“安全”图像,否则 Internet Explorer 会抱怨显示不安全的内容。因此,我尝试使用 Zend_Http_Client
请求下载它们(然后链接到本地文件),但我似乎无法做到这一点。
该 URI 应该有效,因为我可以单击此链接并查看图像:
这是我正在使用的代码:
$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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
$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.