tags:

views:

67

answers:

2

Is there a function in PHP to go out to a separate URL and insert whats returned into the page? The include() function is only for local files right? Would I have to use javascript on the client side to accomplish this? Thanks, Brian

A: 

Blixt's answer requires you to have fopen wrappers enabled. If you don't, you can use cURL. It's more work, but familiarity with cURL is a nice asset, anyway...

grossvogel
+5  A: 

file_get_contents will work, but it is relatively fragile. If the the web site is slow to response or issues a redirect, then file_get_contents will fail. file_get_contents is also a pain to put error capture around. You should use curl instead.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/page.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$page_contents = curl_exec($ch);
if ( $page_contents ===false ) {
    // Do something fancier than this
    echo curl_errno($ch).' '.curl_error($ch);
}
curl_close($ch);
Brent Baisley
+1 -- This is the proper solution to this question, mine was just a quick and dirty solution.
Blixt