file_get_contents网址现在太长了

时间:2013-06-18 20:06:45

标签: php

我相信我的托管公司最近可能会因为之前的工作而改变了。但是,它们毫无用处。

我使用file_get_contents加载到文件中..说实话,它是代码包的一部分,我不是100%它做的。但是,url相当长,它只是回显文件的结果:

$custom = getRealIpAddr()."|||||".$_SESSION['cart']."|||||".makeSafe($_GET['i'])."|||||".$lang;
$pphash = create_paypal_hash(makeSafe($_SESSION['cart']), '', create_password('####'), $custom);
$tosend = base64_encode(urlencode($pphash));
$cgi = "http://www.***********.com/pl/b.pl?a=".$tosend; // TEST LINE
echo file_get_contents($cgi);

这导致一个大约390个字符的URL ..如果我将其减少到大约360个字符,它工作正常 - 但这不是解决方案,因为我丢失了一些传递到文件中的GET数据。

我的主机上可能发生什么变化的想法现在导致url超过360个字符会抛出403禁止错误?

我也尝试了curl方法 - 它也给出了相同的结果:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $cgi);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
echo $output;

1 个答案:

答案 0 :(得分:2)

来自:http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.2.1

服务器应该谨慎依赖于长度超过255个字节的URI,因为某些较旧的客户端或代理实现可能无法正确支持这些长度。

这意味着您需要避免使用超过255的GET。

正如您所注意到的,某些服务器(您的服务器)不会超过255(在您的情况下为360)。

使用POST。

使用CURL:

$url = 'http://www.example.com';
$vars = 'var1=' . $var1 . '&var2=' . $var2;

$con = curl_init($url);
curl_setopt($con, CURLOPT_POST, 1);
curl_setopt($con, CURLOPT_POSTFIELDS, $vars);
curl_setopt($con, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($con, CURLOPT_HEADER, 0);
curl_setopt($con, CURLOPT_RETURNTRANSFER, 1);

$re = curl_exec($con);

没有CURL:

function do_post_request($url, $data, $optional_headers = null)
{
  $params = array('http' => array(
              'method' => 'POST',
              'content' => $data
            ));
  if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
  }
  $ctx = stream_context_create($params);
  $fp = @fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
  }
  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
  }
  return $response;
}