php file_get_contents() 和 Google Chart API 的字符限制?

发布于 2024-09-17 05:30:13 字数 2271 浏览 1 评论 0原文

我正在解决的具体问题是使用 Google 图表 API 启用 https,以及在 url 字符串上使用 php file_get_contents 时可能存在的字符限制。让我带你了解一下到底发生了什么。我使用网上的一些教程取得了很好的进展,特别是启用 https。我正在使用本教程中的“基本方法”:

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

我有一个 Chart.php 文件,其中包含以下代码:

<?php

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

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

我从我的主页调用此文件,并向其传递一个“测试”Google 图表 URL(我使用了许多不同的 URL),即 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');

为了在我的主页中显示图像,我使用以下代码:

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

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

$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 个字符长。

那么这是对 get_file_contents() 的限制吗?我已经将 cURL 作为替代方案,但它的具体细节超出了我的想象。有人可以确认字符限制吗?如果可能的话,提出一些建议?

非常感谢, 尼尔

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梓梦 2024-09-24 05:30:13

编辑:与我下面所述不同,这可能不是服务器问题:Apache 对 GET 字符串的限制据说是 大约 4000 字节。不过,我建议的解决方法仍然有效,因此我保留这个答案。

放入 GET 字符串中的数据量非常大,可能是服务器端限制(Apache 处理请求),也可能是客户端限制(file_get_contents 发送请求)。

我会寻找一种替代方法来执行此操作,例如,通过使用随机密钥将长 URL 存储在会话变量中:

$_SESSION["URL_1923843294284"] = $loooooong_url;

并在 URL 中传递该随机密钥:

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

更新: file_get_contents() 根据这个问题。这很可能是服务器问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文