What are the different methods, such as fopen() or curl, that can be used to read external pages in PHP?
To read external pages in PHP, you can use methods like fopen() or curl. These functions allow you to open a connection to a remote server and retrieve the contents of a webpage.
// Using fopen()
$handle = fopen("http://www.example.com", "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
echo $buffer;
}
fclose($handle);
}
// Using curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
Keywords
Related Questions
- What are the differences between using popen and other file writing functions like fopen or file_put_contents in PHP?
- How can PDO be utilized to improve security and efficiency in PHP database operations?
- What are some best practices for handling HTML output within PHP scripts to avoid errors like unexpected end errors?