使用guzzle客户端获取元标签

时间:2019-04-09 17:32:34

标签: php guzzle

我有一个代码:

$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'http://example.com');
$meta $response->body(true)['meta']; //get meta description

我需要从网站获取元描述内容。我该怎么办?我的解决方案无法正常工作。我收到错误消息:未定义索引元

2 个答案:

答案 0 :(得分:0)

您不需要食尸鬼

$tags = get_meta_tags('http://www.example.com/');

echo $tags['author'];
echo $tags['keywords'];     
echo $tags['description'];

$content = $response->body();
$tags = get_meta_tags($content);

答案 1 :(得分:0)

使用get_meta_tags函数

ini_set('user_agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1');
$meta_tags = get_meta_tags('www.example.com');

使用curl

$user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://example.com");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$data = curl_exec($ch);
curl_close($ch);

print_r($data);