Buscando uma página na internet
<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://example.com/"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); ?>
Alternativa para a função file_get_contents
file_get_contents
<?php $file_contents = file_get_contents('http://example.com/'); // display file echo $file_contents; ?>
Aplicando cURL
<?php $ch = curl_init(); $timeout = 5; // set to zero for no timeout curl_setopt ($ch, CURLOPT_URL, 'http://example.com'); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); curl_close($ch); // display file echo $file_contents; ?>
Aplicando cURL (alternativa)
<?php $site_url = 'http://example.com'; $ch = curl_init(); $timeout = 5; // set to zero for no timeout curl_setopt ($ch, CURLOPT_URL, $site_url); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); ob_start(); curl_exec($ch); curl_close($ch); $file_contents = ob_get_contents(); ob_end_clean(); echo $file_contents; ?>
Obtendo dados binários
Imagem
- Este script recupera uma imagem remota e atribui os dados binarios na variável $image antes de emitir a imagem para o navegador:
<?php $image_url = "http://example.com/image.jpg"; $ch = curl_init(); $timeout = 0; curl_setopt ($ch, CURLOPT_URL, $image_url); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // Getting binary data curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); $image = curl_exec($ch); curl_close($ch); // output to browser header("Content-type: image/jpeg"); print $image; ?>
Alternativa para a função file
Função File
<?php $lines = file('http://example.com/'); // display file line by line foreach($lines as $line_num => $line) { echo "Line # {$line_num} : ".htmlspecialchars($line)."<br />\n"; } ?>
Aplicando cURL
<?php $ch = curl_init(); $timeout = 5; // set to zero for no timeout curl_setopt ($ch, CURLOPT_URL, 'http://example.com'); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); curl_close($ch); $lines = array(); $lines = explode("\n", $file_contents); // display file line by line foreach($lines as $line_num => $line) { echo "Line # {$line_num} : ".htmlspecialchars($line)."<br />\n"; } ?>