我的应用程序使用 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
. 这是我为了下载 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";
}