I am attempting to open a specified page automatically upon loading a PHP page, but not by means of redirecting, is file_get_contents suitable for this? Also I have tried curl with the following, however I do not see any responses:
curl_setopt($ch=curl_init(), CURLOPT_URL, "http://www.google.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
many thanks in advance
cURL is perfect for that situation. If you just want to to display the page, you don't need to set curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
as the default behaviour is to display the result.
Check for errors:
if ($response === false) {
echo curl_error($ch);
}
If all you want is to get the contents of the page, file_get_contents
will do.
Using file_get_contents
would do the trick but you need to enable allow_url_fopen.
You can also use CURL but your code is not working becasue you are missing CURLOPT_FOLLOWLOCATION
Full Script
curl_setopt($ch=curl_init(), CURLOPT_URL, "http://www.google.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
echo curl_exec($ch);
curl_close($ch);
is file_get_contents suitable for this?
Yes, if your server allow you to use this function it is suitable
<?php
echo "<h2>Here we go</h2>";
$content = file_get_contents("http://www.google.com");
echo $content;
?>
Also you are able to modify the $content