从外部服务器获取页面

发布于 2024-07-26 12:30:54 字数 115 浏览 1 评论 0原文

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

任何想法将不胜感激。

干杯。

I want to pass a function a string, which takes that string tacks it onto url. Then goes to that url and then returns the page to my server so I can manipulate it with JS.

Any Ideas would be much appreciated.

cheers.

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

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

发布评论

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

评论(3

往事风中埋 2024-08-02 12:30:55

使用 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;
}

Using CURL would probably be easiest but I prefer to do stuff myself. This will connect to a given address and return the contents of the page. It will also return the headers, though, so watch out for that:

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;
}
dawn曙光 2024-08-02 12:30:54

如果您的 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;
}

这当然不会影响原来的页面。

If your fopen_wrappers are enabled, you can use file_get_contents() to retrieve the page, and then insert JavaScript into the content before echoing it as output.

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

This of course won't affect the original page.

冬天的雪花 2024-08-02 12:30:54

您应该能够使用 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);
}

You should be able to use fopen() for what you want. It can accept URLs.

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