【问题标题】:Get page from external server从外部服务器获取页面
【发布时间】:2009-07-07 19:22:53
【问题描述】:

我想向函数传递一个字符串,该字符串将该字符串附加到 url 上。然后转到该 url,然后将页面返回到我的服务器,以便我可以使用 JS 对其进行操作。

任何想法将不胜感激。

干杯。

【问题讨论】:

  • 请澄清。这让你的 10k 读者感到困惑。

标签: php request xss fopen


【解决方案1】:

如果您的 fopen_wrappers 已启用,您可以使用 file_get_contents() 检索页面,然后将 JavaScript 插入内容中,然后将其作为输出回显。

$content = file_get_contents('http://example.com/page.html');
if( $content !== FALSE ) {
  // add your JS into $content
  echo $content;
}

这当然不会影响原始页面。

【讨论】:

    【解决方案2】:

    您应该能够使用fopen() 来满足您的需求。它可以接受 URL。

    echo "<script type='text/javascript' src='myjavascript.js'></script>";
    $handle = @fopen("http://www.example.com/", "r");
    if ($handle) {
        while (!feof($handle)) {
            $buffer = fgets($handle, 4096);
            echo $buffer;
        }
        fclose($handle);
    }
    

    【讨论】:

      【解决方案3】:

      使用 CURL 可能是最简单的,但我更喜欢自己做事。这将连接到给定的地址并返回页面的内容。不过,它也会返回标头,因此请注意:

      function do_request ($host, $path, $data, $request, $specialHeaders=null, $type="application/x-www-form-urlencoded", $protocol="", $port="80")
      {
              $contentlen = strlen($data);
              $req = "$request $path HTTP/1.1\r\nHost: $host\r\nContent-Type: $type\r\nContent-Length: $contentlen\r\n";
              if (is_array($specialHeaders))
              {
                  foreach($specialHeaders as $header)
                  {
                      $req.=$header;
                  }
              }
              $req.="Connection: close\r\n\r\n";
              if ($data != null) {
                  $req.=$data;
              }
              $fp = fsockopen($protocol.$host, $port, $errno, $errstr);
              if (!$fp) {
                  throw new Exception($errstr);
              }
              fputs($fp, $req);
              $buf = "";
              if (!feof($fp)) {
                  $buf = @fgets($fp);
              }
              return $buf;
      }
      

      【讨论】:

      • 是的,file_get_contents() 可能也更容易,但如果 fopen_wrappers 未启用,这将起作用。
      猜你喜欢
      • 2012-12-03
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 2020-05-07
      • 2022-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多