
Get content from web URL in laravel
Get content from web URL in laravel
With this article, we will examine several different instances of how to solve the "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.-
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; });
Output :
{"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.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given
- Route not defined in Laravel
- First and last item of the array using foreach iteration in laravel blade
- Call to undefined method App\Models\User::follow()
- How to insert multiple rows in mysql using loop in laravel?
- Add a subselect based on relationship using withAggregate method
- Link storage folder in laravel 8
- How to get last month records in Laravel
- How to upload files to amazon s3 bucket using Laravel
- Laravel clone model
- Delete file from amazon s3 bucket using Laravel
- After image selected get validation error in laravel
- Get today records in Laravel
- How to get the id of last record from collection object in laravel view
- Get id of last inserted record in laravel
- Input file with max size validation in laravel
- Get only 10 records from table in laravel
- How to add foreign key in laravel using migration
- Call to undefined method Illuminate\Support\Facades\Request::all()
- The use statement with non-compound name 'DB' has no effect
- Ajax GET request in laravel
- Generate random string lowercase in Laravel
- Remove public from url in laravel project
- Class 'App\Http\Controllers\User' not found
- Post model with title and body in laravel 8