
Get content from web URL in laravel
You can get the content from a web URL in laravel. You can get all the meta tags within
and all the html tags within tag and get the content of these tags and pass them to variables and return as output.
Answers 1
-
Get title, description and image from meta tags using website url in laravel
--PATH routes\web.phpRoute::get('/get-content-url', function(){ $url = 'https://oscarliang.com/'; $resp = []; $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 = ''; if($nodes->length > 0){ $title = $nodes->item(0)->nodeValue; } // Parse DOM to get Meta Description $metas = $dom->getElementsByTagName('meta'); $body = ""; $imageURL = ""; for ($i = 0; $i < $metas->length; $i ++) { $meta = $metas->item($i); if ($meta->getAttribute('name') == 'Description' || $meta->getAttribute('name') == 'description') { $body = $meta->getAttribute('content'); } if ($meta->getAttribute('property') == 'og:image:url') { $imageURL = $meta->getAttribute('content'); }elseif($meta->getAttribute('property') == 'og:image'){ $imageURL = $meta->getAttribute('content'); } } $images = $dom->getElementsByTagName('img'); if(!isset($imageURL) && $images->length > 0){ $imageURL = $images->item(0)->getAttribute('src'); } $output = array( 'title' => $title, 'image_url' => $imageURL, 'body' => preg_replace('/[^\00-\255]+/u', '', $body) ); return $output; });
0Output :
{"title":"Oscar Liang - FPV Drone Tutorials and Reviews",
"image_url":"https:\/\/oscarliang.com\/ctt\/uploads\/2021\/03\/oscarliang-fpv-drone-tutorial-review-blog-quadcopter-rc.jpg",
"body":"FPV Drone Tutorials and Reviews, a blog for RC hobbyists to get latest information about quadcopter
and multirotor products and technology development, delivered to you by Oscar Liang."}This code snippet will return the title, description and image url from meta tags of the web page. If image does not exist with the meta property og:image and og:image:url within meta tags then it will get the first image from inside of the body tag of the web page.
Random Code Snippet Queries: Laravel
- JQuery each loop on json response after ajax in laravel
- Laravel URL validation not working
- How to upload files to amazon s3 bucket using Laravel
- Illuminate\Database\QueryException could not find driver
- Insert dummy data in users table Laravel
- How to add class to tr in table using foreach in laravel
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"
- Laravel 9 route group with controller
- Call to a member function update() on null
- How to get query string value in laravel
- Call to a member function pluck() on null
- How to include header file in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- Import/Use Storage facade in laravel
- PhpMyAdmin - Error The mysqli extension is missing
- How to add dynamic page title in Laravel view
- Update existing pivot table data in laravel
- Laravel get all records with pagination
- How to send email in laravel
- Laravel specific table Migration
- Laravel 5.4 save data to database
- Laravel 7 login error message not showing
- InRandomOrder() method with example in laravel
- How to decrypt laravel password
- How to pass data to route in laravel?