views:

32

answers:

2

Hi, the specific issue I am working on is enabling https with Google charts API, and a possible character limit when using php file_get_contents on a url string. Let me take you through what is going on. I have made good progress using some tutorials on the net, specifically to enable the https. I am using their 'basic method' from this tutorial:

http://webguru.org/2009/11/09/php/how-to-use-google-charts-api-in-your-secure-https-webpage/

I have a chart.php file with this code in it:

<?php

$url = urldecode($_GET['api_url']);

$image_contents = file_get_contents($url);
echo $image_contents;
exit;
?>

I am calling this file from my main page, passing a 'test' Google chart URL (I have used many different ones) to it, which is 513 chars long:

$chartUrl = urlencode('http://chart.apis.google.com/chart?chxl=0:|Jan|Feb|Mar|Jun|Jul|Aug|1:|100|75|50|25|0&amp;chxt=x,y&amp;chs=300x150&amp;cht=lc&amp;chd=t:60.037,57.869,56.39,51.408,42.773,39.38,38.494,31.165,30.397,26.876,23.841,20.253,16.232,13.417,12.677,15.248,16.244,13.434,10.331,10.58,9.738,10.717,11.282,10.758,10.083,17.299,6.142,19.044,7.331,8.898,14.494,17.054,16.546,13.559,13.892,12.541,16.004,20.026,18.529,20.265,23.13,27.584,28.966,31.691,36.72,40.083,41.538,42.788,42.322,43.593,44.326,46.152,46.312,47.454&amp;chg=25,25&amp;chls=0.75,-1,-1');

To display the image in my main page I am using this code:

<img src="https://mysite.com/chart.php?api_url=&lt;?php echo $chartUrl; ?>" />

The example $chartUrl string should display nothing. It will work fine until the $chartUrl string exceeds 512 characters in length (unencoded). For example if you use this string below (512 chars long):

$chartUrl = urlencode('http://chart.apis.google.com/chart?chxl=0:|Jan|Feb|Mar|Jun|Jul|Aug|1:|100|75|50|25|0&amp;chxt=x,y&amp;chs=300x150&amp;cht=lc&amp;chd=t:60.037,57.869,56.39,51.408,42.773,39.38,38.494,31.165,30.397,26.876,23.841,20.253,16.232,13.417,12.677,15.248,16.244,13.434,10.331,10.58,9.738,10.717,11.282,10.758,10.083,17.299,6.142,19.044,7.331,8.898,14.494,17.054,16.546,13.559,13.892,12.54,16.004,20.026,18.529,20.265,23.13,27.584,28.966,31.691,36.72,40.083,41.538,42.788,42.322,43.593,44.326,46.152,46.312,47.454&amp;chg=25,25&amp;chls=0.75,-1,-1');

The chart should show up. The difference between the strings is one character. The 'real' Google chart API string that I will be using in the final version is about 1250 chars long.

So is this a limit on get_file_contents()? I have looked at cURL as an alternative, but its specifics go over my head. Can someone confirm the char limit, and if possible make some suggestions?

Many thanks, Neil

A: 

Edit: Unlike I stated below, this is probably not a server problem: Apache's limit on GET strings is said to be around 4000 bytes. The workaround I suggest is still valid, though, so I'm leaving this answer in place.

This is an awful lot of data to put in a GET string, and could be a server side limitation (Apache handling the request) as much as a client side one (file_get_contents sending the request).

I would look for an alternative way of doing this, e.g. by storing the long URL in a session variable with a random key:

$_SESSION["URL_1923843294284"] = $loooooong_url;

and pass that random key in the URL:

<img src="https://mysite.com/chart.php?api_url=1923843294284" />

Update: There does not seem to be a native length limit to file_get_contents() according to this question. This may well be a server issue.

Pekka
Thank you for your quick response. I understand the idea, but I can not get it to work in practice. I am using your code as an example (with urls replaced), I have enabled sessions and have tried online and localhost:$_SESSION["URL_1923843294284"] = $chartUrl;<img src="https://mysite.com/chart.php?api_url=URL_1923843294284" />I have average PHP knowledge, so I accept that I may be doing something wrong, and I guess it is to do with the parsing of the api_url key. I have echoed the session variable, which will work, but again the char limit issue raises its ugly head!
Neil
@Neil you need to initialize a session using `session_start()` in both scripts. Other than that, it should work.
Pekka
Awww @Neil I realize only now that maybe, your final URL that gets passed to Google is already > 512 bytes long! In that case, my idea won't do you any good. I apologize. In that case, you may really have to resort to CURL and test whether it is more lenient.
Pekka
A: 

@ Pekka

Yes you are right. I just implemented your method, which is a good idea, but ultimately there is still the char limit issue as you have stated.

I am going to give a bit more time to this as it is becoming a deal breaker for using Google Charts on the secure site I am working on.

Neil
@Pekka i implemented cURL and got it working. I started to notice a strange parse error on the string I was using too. The labels I had added to the graph had spaces in them, which would parse as a straight forward url but not when added via a string.I amended that and the graph now works with cURL, randomised session ids and all the goodness I needed.
Neil