How can I include or embed an external webpage using PHP?

Solution:1
You can use cURL in PHP to fetch the contents of a webpage. Here’s a reusable function:


/**
 * Fetch a web resource (HTML, XML, images, etc.) from a URL.
 * Returns an array containing HTTP response headers and content.
 */
function get_web_page($url)
{
    $options = [
        CURLOPT_RETURNTRANSFER => true,     // return web page as string
        CURLOPT_HEADER         => false,    // exclude headers in output
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "spider", // custom user agent
        CURLOPT_AUTOREFERER    => true,     // auto set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // connection timeout
        CURLOPT_TIMEOUT        => 120,      // response timeout
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    ];

    $ch      = curl_init($url);
    curl_setopt_array($ch, $options);
    $content = curl_exec($ch);
    $err     = curl_errno($ch);
    $errmsg  = curl_error($ch);
    $header  = curl_getinfo($ch);
    curl_close($ch);

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;

    return $header;
}

Notes:

If assets like images or stylesheets use relative URLs (e.g., /image.jpg), you may need to rewrite them into absolute URLs (http://domain.com/image.jpg).

To extract just specific parts (likeor), you can use DOM parsing (DOMDocument, simplexml, or XPath) if the HTML is well-formed. Otherwise, regex can work for simpler tasks.

Most shared hosting environments already have cURL enabled.

Solution 2: Using file_get_contents


$content = file_get_contents("https://example.com");
echo $content;

However, this approach comes with some important caveats:

Relative links (e.g., /style.css or /image.jpg) will break unless you rewrite them to absolute URLs.

Security risks such as cross-site scripting (XSS) can arise if you directly output external content.

Solution 3: Extracting Images with Regex

If your goal is to grab images from the fetched HTML, you can use a regex with preg_match_all():


function extractImage($results) {
    // Regex pattern to match image URLs (jpg, jpeg, gif)
    $pattern = '/http:\/\/.+\.(jpeg|jpg|gif)/i';  
    preg_match_all($pattern, $results, $matches);

    $url = null;

    foreach ($matches[0] as $image) {
        // Prepend your domain if needed
        $url = "http://saxtorinc.com/$image";
    }

    return $url; // Returns the last matched image
}

Notes:

The function currently returns only the last image URL found. If you want all images, you should return the whole $matches[0] array instead.

The domain (http://saxtorinc.com/) must be set manually, or you can dynamically detect and prepend it if the HTML uses relative paths.

Regex works, but for more complex HTML parsing, DOMDocument or XPath is more reliable.