Get page from external server

I want to pass a string to a function that takes this string by binding it to url. It then goes to that URL and then returns the page to my server, so I can manipulate it using JS.

Any ideas would be highly appreciated.

greetings.

+3
source share
3 answers

If your fopen_wrappers are included, you can use file_get_contents () to retrieve the page, and then paste JavaScript into the content before repeating it as an output.

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

This, of course, will not affect the original page.

+9
source

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);
}
0

Using CURL will probably be the easiest, but I prefer to do things myself. This will connect to a specific address and return the contents of the page. However, it also returns headers, so be careful:

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;
}
0
source

Source: https://habr.com/ru/post/1712037/


All Articles