views:

389

answers:

2

I'm trying to load charts as images in a secured site. An example of a Google Chart image over https would be this:

http://www.google.com/chart?cht=lc&chs=200x125&chd=s:helloWorld

The problem is that while you can load an image like this by directly clicking the link, you can't include it as an image in your page. It just won't load.

Any idea on how to bypass this? Or solution generally using PHP?

+2  A: 

Looks like google is blocking https requests for charts that have a Referrer: header set.

[tla ~]$ curl 'http://www.google.com/chart?cht=lc&chs=200x125&chd=s:helloWorld' 2>/dev/null | file -
/dev/stdin: PNG image, 200 x 125, 8-bit/color RGB, non-interlaced
[tla ~]$ curl 'https://www.google.com/chart?cht=lc&chs=200x125&chd=s:helloWorld' 2>/dev/null | file -
/dev/stdin: PNG image, 200 x 125, 8-bit/color RGB, non-interlaced
[tla ~]$ curl -H 'Referer: http://stackoverflow.com' 'http://www.google.com/chart?cht=lc&chs=200x125&chd=s:helloWorld' 2>/dev/null | file -
/dev/stdin: PNG image, 200 x 125, 8-bit/color RGB, non-interlaced
[tla ~]$ curl -H 'Referer: http://stackoverflow.com' 'https://www.google.com/chart?cht=lc&chs=200x125&chd=s:helloWorld' 2>/dev/null | file -
/dev/stdin: ASCII HTML document text, with very long lines
MikeyB
It has come to my attention that I like to use empirical methods ;)
MikeyB
+3  A: 

Google does not support Charts over HTTPS ...

I had the same problem.

http://groups.google.com/group/google-chart-api/browse_thread/thread/95c463d88cf3cfe4

You could however use PHP or .net to create a proxy page to filter your google HTTP link via a HTTPS connection to solve such a problem.

Here is a simple PHP proxy I've used ...

<?php
    // PHP Proxy
    // Loads a XML from any location. Used with Flash/Flex apps to bypass security restrictions
    // Author: Paulo Fierro
    // January 29, 2006
    // usage: proxy.php?url=http://mysite.com/myxml.xml

    $session = curl_init($_GET['url']);                    // Open the Curl session
    curl_setopt($session, CURLOPT_HEADER, false);          // Don't return HTTP headers
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);   // Do return the contents of the call
    $xml = curl_exec($session);                            // Make the call
    header("Content-Type: text/xml");                  // Set the content type appropriately
    echo $xml;        // Spit out the xml
    curl_close($session); // And close the session
?>
Justin Jenkins