jeudi 15 avril 2021

Is there any php function to find any url title or description?

I am new in data scraping, i am working on url to title scraping, actually i want to write a funtion that take a url/link as request and in return i get <title> </title> , og:title , og:description etc. all meta property

i am trying this funtion for scrape only title

/**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @throws ValidationException
     */
    public function getTitle(Request $request)
    {
        $this->validate($request, [
            'link' => 'required',
        ]);

        $link = $request->input('link');

        $str = @file_get_contents($link);
        if(strlen($str)>0){
            $str = trim(preg_replace('/\s+/', ' ', $str));
            preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title);
            $result = $title[1];
        }

        return Response::json([
            'message' => 'Get title',
            'data'    => $result,
        ], \Symfony\Component\HttpFoundation\Response::HTTP_OK);
    }

route

Route::post('request-title', 'BuyShipRequestController@getTitle');

Example what i request in input field:

Amazon-url

and what i want to my reutrn response

<title>Amazon.com: Seagate Portable 2TB External Hard Drive Portable HDD – USB 3.0 for PC, Mac, PS4, &amp; Xbox - 1-Year Rescue Service (STGX2000400): Computers &amp; Accessories</title>

and

<meta name="description"/> , <meta name="title"/>, <meta name="keywords" /> , link 

in return response i want only those meta properties content or value



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire