0

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";
}
Community
  • 1
  • 1
Andrew
  • 227,796
  • 193
  • 515
  • 708

1 Answers1

0

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.

Andrei Serdeliuc ॐ
  • 5,828
  • 5
  • 39
  • 66
  • the problem is that the Google Chart URL contains characters that are usually invalid (such as the colon character), so it is failing validation – Andrew Aug 16 '10 at 17:19
  • Have you tried replacing those characters with their hex value? (i.e.: ":" turns into "%3A"). You can always check what each character should be by having a look at asciitable.com – Andrei Serdeliuc ॐ Aug 17 '10 at 06:23
  • I think the %3A worked. Change your answer to say "replace colons with %3A" and I will accept your answer – Andrew Aug 19 '10 at 19:03