51016

Saving Google Chart image to file using PHP

Question:

I have a script which generates Google Chart images for clients. It works fine displaying in HTML but I now need to save each chart locally as a .png.

If I try:

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

then I always receive

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

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

An example of my Google chart $url is below. I've hexed out the colons and pipe symbols but it did not fix the issue:

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

Answer1:

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; ?>

Answer2:

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

Recommend

  • How do I fix the input allowed into an input box?
  • Javadoc generation for Android
  • Java graph library [duplicate]
  • Pie-in-pie (Donut) charts
  • Cannot read property 'addEvent' of undefined
  • Greek letters in a GUI - PYTHON
  • Git cherry-Pick a batch of commits from a file
  • jquery pie chart with drilldown/clickable pie parts
  • console.log printing statements in the wrong order for learnyounode node.js tutorial
  • How to compare the string in C++?
  • str_replace with line break? [closed]
  • Why does _GET in PHP wrongly decodes slash?
  • How do I get the post request with express js?
  • Objective-C : getting error on console while trying to display app on ipad device?
  • Determining the length of a read stream in node js
  • Sympy: working with equalities manually
  • Unzip archive in .Net CORE 1.0
  • Streaming screenshots over WebRTC as a video stream from iOS
  • How to implement Deep Linking in Roku SG application?
  • How to read piped content in C?
  • How do I shift the decimal place in Python?
  • Application level floating views with navigation in Android
  • Row to Column conversion in Talend
  • jQuery Orbit - How to make a Random Slideshow?
  • Play WS (2.2.1): post/put large request
  • Bad request using file_get_contents for PUT request in PHP
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Can I make an Android app that runs a web view in Chrome 39?
  • XCode can't find symbols for a specific iOS library/framework project
  • php design question - will a Helper help here?
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • AngularJs get employee from factory
  • need help with bizarre java.net.HttpURLConnection behavior
  • Turn off referential integrity in Derby? is it possible?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • Authorize attributes not working in MVC 4
  • JaxB to read class hierarchy