char限制与php file_get_contents()和Google图表API? [英] char limit with php file_get_contents() and Google Chart API?

查看:139
本文介绍了char限制与php file_get_contents()和Google图表API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发的具体问题是启用https与Google图表API,并且当在URL字符串使用php file_get_contents时可能的字符限制。让我带你通过发生了什么。我已经取得良好的进展使用一些教程在网上,特别是启用https。我使用本教程中的基本方法:

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 /

我有一个包含此代码的chart.php文件:

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

我从主页调用此文件,传递一个'test'使用许多不同的),它是513 chars long:

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

示例$ chartUrl字符串应该不显示任何内容。它将工作正常,直到$ chartUrl字符串超过512个字符的长度(未编码)。例如,如果您使用以下这个字符串(512个字符长):

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');

图表应该会出现。字符串之间的区别是一个字符。我将在最终版本中使用的真实Google图表API字符串约为1250个字符长。

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.

这是对get_file_contents()的限制吗?我看过cURL作为替代,但它的细节超过我的头。

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?

非常感谢,
Neil

Many thanks, Neil

推荐答案


编辑:不像我在下面说的,这可能不是一个服务器问题:Apache对GET字符串的限制被称为大约4000个字节。我建议的解决方法仍然有效,因此我将此答案留在原地。

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.

这是一个可怕的大量数据放入GET字符串,并且可以是服务器端限制(Apache处理请求)尽可能多的客户端端(file_get_contents发送请求)。

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).

我会寻找一种替代方法。将长URL存储在具有随机键的会话变量中:

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" />

更新:似乎没有本机长度限制file_get_contents ),根据此问题。这可能是一个服务器问题。

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.

这篇关于char限制与php file_get_contents()和Google图表API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆