![]() |
The following PHP code shows how to get page titles and other meta details by using PHP CURL. In this code, I initialized the CURL object and set the URL to be accessed with the reference of this object. The CURL script will return the HTML content of the remote page. After getting the HTML content, we need to parse the HTML by referring to the title , meta and img tag names to get the page title, description and the image URLs, respectively. These data are encoded into a JSON array and returned as the AJAX response. |
Just download index.php and run into your website.
You can change the url horje.com and put your once.
<?php
$url = "https://horje.com/learn/757/clipbucket"; // Extract HTML using curl $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $data = curl_exec($ch); curl_close($ch); // Load HTML to DOM Object $dom = new DOMDocument(); @$dom->loadHTML($data); // Parse DOM to get Title $nodes = $dom->getElementsByTagName('title'); $title = $nodes->item(0)->nodeValue; // Parse DOM to get Meta Description $metas = $dom->getElementsByTagName('meta'); $body = ""; for ($i = 0; $i < $metas->length; $i ++) { $meta = $metas->item($i); if ($meta->getAttribute('name') == 'description') { $body = $meta->getAttribute('content'); } } // Parse DOM to get Images $image_urls = array(); $images = $dom->getElementsByTagName('img'); for ($i = 0; $i < $images->length; $i ++) { $image = $images->item($i); $src = $image->getAttribute('src'); $m_i .= ''.$src.'</br>'; }
echo $title;
echo '</br>';
echo $body;
echo '</br>';
echo $src;
echo '</br>';
echo $m_i;
?>
Simplely run following codes to your site by chaning http url.
<?php
// Web page URL
$url = 'https://horje.com';
// Extract HTML using curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
// Load HTML to DOM object
$dom = new DOMDocument();
@$dom->loadHTML($data);
// Parse DOM to get Title data
$nodes = $dom->getElementsByTagName('title');
$title = $nodes->item(0)->nodeValue;
// Parse DOM to get meta data
$metas = $dom->getElementsByTagName('meta');
$description = $keywords = '';
for($i=0; $i<$metas->length; $i++){ $meta = $metas->item($i); if($meta->getAttribute('name') == 'description'){ $description = $meta->getAttribute('content'); } if($meta->getAttribute('name') == 'keywords'){ $keywords = $meta->getAttribute('content'); } if($meta->getAttribute('property')=='og:image'){ $meta_og_image = $meta->getAttribute('content');
}
}
$src = preg_match('/<img.+src=[\'"]http(?P<src>.+?)[\'"].*>/i', $data, $result);
$img = 'http'.$result[1].'';
echo "Title: $title". '<br/>';
echo "Description: $description". '<br/>';
echo "Keyword: $keywords". '<br/>';
echo "OG Image: $meta_og_image". '<br/>';
echo "Imgage: $img". '<br/>';
?>
php curl |
Type
: |
Develop |
Category
: |
Web Tutorial |
Sub Category
: |
PHP Extract Tutorial |
Uploaded by
: |
Admin |