Lấy nội dung của 1 trang web = file_get_contents

<?php
function get_web_page( $url )
{
$options = array( 'http' => array(
'user_agent'    => 'spider',        // who am i
'max_redirects' => 10,              // stop after 10 redirects
'timeout'       => 120,             // timeout on response
) );
$context = stream_context_create( $options );
$page    = @file_get_contents( $url, false, $context );
$result  = array( );
if ( $page != false )
$result['content'] = $page;
else if ( !isset( $http_response_header ) )
return null;    // Bad url, timeout

// Save the header
$result['header'] = $http_response_header;

// Get the *last* HTTP status code
$nLines = count( $http_response_header );
for ( $i = $nLines-1; $i >= 0; $i-- )
{
$line = $http_response_header[$i];
if ( strncasecmp( "HTTP", $line, 4 ) == 0 )
{
$response = explode( ' ', $line );
$result['http_code'] = $response[1];
break;
}
}

return $result;
}
print_r(get_web_page("http://thuongtin.net"));
?>

Popularity: 18% [?]


  1. No comments yet.

  1. No trackbacks yet.

Chọn kiểu gõ: Tự động TELEX VNI Tắt

Bad Behavior has blocked 48 access attempts in the last 7 days.