Saving an image on Google Chart to a file using PHP

I have a script that generates google chart images for clients. It works fine in HTML, but now I need to save each diagram locally as .png.

If I try:

$imageData = file_get_contents($url); file_put_contents('C:/xampp/htdocs/image.png',$imageData);

then i always get

 [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 

The script works fine for regular images, but not for Google charts. Same result if I use cURL to save image.

Below is an example of my google url. I crossed out the colons and pipe characters, but this did not solve the problem:

 http://chart.apis.google.com/chart?cht=lc&chxt=x,y&chs=700x400&chco=76A4FB,FF9900&chg=0,8.3333333&chdl=Visits|Unique%20visits&chls=3|3&chma=40,20,20,30&chxr=1,0,33411&chds=0,33411&chd=t%3A33411,33411,33411,33411,33411,33411,33411,33411,33411,33411,33411,33411|33411,33411,33411,33411,33411,33411,33411,33411,33411,33411,33411,33411&chxl=0%3A|Jan-10|Feb-10|Mar-10|Apr-10|May-10|Jun-10|Jul-10|Aug-10|Sep-10|Oct-10|Nov-10|Dec-10 
+1
source share
2 answers

CURL example:

 <?php $ch = curl_init("www.example.com/curl.php?option=test"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); echo $output; ?> 
+1
source

I managed to get this to work at the end - it seems that spaces in the label names are causing errors, replacing% 20 did the trick

0
source

All Articles