使用file_get_contents函数,我想获得前几个谷歌搜索结果,而不是全部

I've used the file_get_contents to get the google results of a string and display to the web page.

$url = "http://www.google.com/search?q=".$str;

$result = file_get_contents($url);

echo $result;

But is it possible to display only the first few results, say 5, instead of all the results ?

To limit the number of Google search results, add the parameter &num=5 to your Google query.

Compare

https://www.google.co.uk/search?site=&source=hp&q=foo

https://www.google.co.uk/search?site=&source=hp&q=foo&num=5