Lakatoi
Lakatoi

Reputation: 27

Pull and load on a server an extern php webpage

I'm a trying to load or see a .php webpage placed outside the hosting server, from a .php in the server. Is it possible? The server has PHP v.7.3.15

With the following code:

<?php
echo file_get_contents('http://XXXXXXXXXXXX');
?>

I get this following error:

Warning: file_get_contents(http://XXXXXXXXXX): failed to open stream: Network is unreachable in XXXXXX

Upvotes: 0

Views: 44

Answers (1)

Patrick Simard
Patrick Simard

Reputation: 2375

Sometimes it's better to use cURL

$url = "http://www.xxxxxxx.xxx";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($curl);
curl_close($curl);

$output should contain the HTML.

If you still don't have the HTML, it's possible the remote server is blocking out bot like requests. Using cURL, you will have better control over how you impersonate a browser to the point the remote server will not make the difference between your script and a real human.

The code above is the basics of it and from there you can add all sorts of curl_setopt() to send in a fake browser ID as well as a disregard of HTTPS.

Here's a few you can use:

    curl_setopt($curl, CURLOPT_COOKIESESSION, true);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt($curl, CURLOPT_VERBOSE, true);
    curl_setopt($curl, CURLOPT_AUTOREFERER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_ENCODING, '');
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36');

In the above $cookie would be the path to a txt file that is writable so it can save the needed data like a real browser.

You can also take a look at your browser headers (in the developer tools) as you navigate on the website and use those same values in your curl request.

Upvotes: 1

Related Questions