使用 Goutte 抓取时 PHP 返回数组

标签 php api goutte laravel-5.1

我正在尝试使用 goutte 返回一组项目,我可以将它们打印出来,但我希望它们在一个数组中,就像 API 一样。这是示例代码。我正在使用 Laravel 5.1。

public function index()
{
    $posts = array();
    $client = new Client();
    $crawler = $client->request('GET', 'http://www.icetimux.com');

    $crawler->filter('h2 > a')->each(function ($node) use ($posts){
        // print $node->text(); //this prints them, needs to return as an array :(
        array_push($posts, $node->text());
    });
    return $posts;
}

我得到的只是一个空数组。

最佳答案

哈哈!我做到了!检查一下!

public function index()
{
    $client = new Client();
    $crawler = $client->request('GET', 'http://www.icetimux.com');

    return $result = $crawler->filter('h2 > a')->each(function ($node){
        return $posts[] = $node->text();
    });
}

关于使用 Goutte 抓取时 PHP 返回数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31269162/

相关文章:

php - MYsql更新新用户问题

php - 使用PHP输入文本时防止表格扩展

php - 来自具有一个公共(public)键的两个其他数组的新数组。有什么优化技巧吗?

php - Goutte/Guzzle 可以强制进入 UTF-8 模式吗?

php - 使用 Symfony DomCrawler 获取表格行

php - 将 utf8 unicode 字符串转换为 PHP 中的 uft8 general ci 而不是来自 MySQL

java - 使用反射修改方法并获得对其局部变量的访问权限

javascript - 未在获取 api 调用时设置 header

android - 新发布的 "Authentication with Google Play Services",通过 GoogleAuthUtil.getToken 获取 token 时出现问题

php - 如何在 Goutte 中设置 cookie?