首页 > 解决方案 > 如何在Laravel 上使用 GuzzleHttp 提取标签

问题描述

标签: laravel

解决方案


您可以使用DOMDocument。例如:

use DOMDocument;

class GuzzleController extends Controller
{
    public function Index(){
        $client = new \GuzzleHttp\Client();        
        $response = $client->request('GET', 'http://google.com');

        $html = (string) $response->getBody(); 

        $dom = new DOMDocument;
        $dom->loadHTML($html);

        $links = $dom->getElementsByTagName('a');

        foreach ($links as $link){
            echo $link->nodeValue;
            echo $link->getAttribute('href');
        }
    }
}

这只是一个例子。理想情况下,您会想要注入这些依赖项。


推荐阅读