char limit with php file_get_contents() and Google Chart API?

孤街醉人 提交于 2019-12-24 00:24:15

问题


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&chxt=x,y&chs=300x150&cht=lc&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&chg=25,25&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=<?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&chxt=x,y&chs=300x150&cht=lc&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&chg=25,25&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


回答1:


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.



来源:https://stackoverflow.com/questions/3609169/char-limit-with-php-file-get-contents-and-google-chart-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!