在laravel 5.4中调用函数内部的一个函数

时间:2017-03-03 11:02:59

标签: php laravel-5.4

我之前的问题是parse the html data to array data in php

参考这个Answer,我在laravel中尝试了同样的事情。它给了我像DOMXPath::query(): Unable to call handler hasClasses()这样的错误然后我使用了类似下面的东西

我的laravel代码:

function hasClasses($attrValue, $requiredClasses)
{
    $requiredClasses = explode(' ', $requiredClasses);
    $classes = preg_split('~\s+~', $attrValue, -1, PREG_SPLIT_NO_EMPTY);

    return array_diff($requiredClasses, $classes) ? false : true;
}


function getScrapedata($keyword = 'laravel')
{
    $client = new Client();
    $client->setHeader('user-agent', "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3");
    $crawler = $client->request('GET', 'http://duckduckgo.com/?q=' . $keyword);
    $string = $crawler->filter('div#links.results')->html();

    $dom = new \DOMDocument;
    $state = libxml_use_internal_errors(true);
    $dom->loadHTML($string);
    libxml_use_internal_errors($state);

    $xp = new \DOMXPath($dom);
    $xp->registerNamespace('php', 'http://php.net/xpath');
    $xp->registerPhpFunctions('hasClasses');

    $mainDivClasses = 'result results_links results_links_deep web-result';
    $childDivClasses = 'links_main links_deep result__body';

    $divNodeList = $xp->query('//div[php:functionString("' . $this->hasClasses('class', $mainDivClasses) . '")]
                               /div[php:functionString("' . $this->hasClasses('class', $mainDivClasses) . '")]');

    $array_data = [];
    foreach ($divNodeList as $divNode) {
        $array_data[] = [
            trim($xp->evaluate('string(./h2/a[@class="result__a"])', $divNode)),
            trim($xp->evaluate('string(.//a[@class="result__snippet"])', $divNode)),
            trim($xp->evaluate('string(.//a[@class="result__url"])', $divNode)),
        ];
    }

    dd($array_data);
}

所以它开始给我另一个错误DOMXPath::query(): Unable to call handler ()

所以实际错误在这里:

$divNodeList = $xp->query('//div[php:functionString("'.$this->hasClasses('class',$mainDivClasses).'")]
                               /div[php:functionString("'.$this->hasClasses('class',$mainDivClasses).'")]');

请有人帮我解决这个问题。如何准确调用函数。?

1 个答案:

答案 0 :(得分:0)

解决此问题的一种方法是将hasClasses方法更改为:

static function hasClasses($attrValue, $requiredClasses)

然后你应该能够做到:

$divNodeList = $xp->query('//div[php:functionString("' . static::class . '::hasClasses", @class, "' . $mainDivClasses . '")]
                   /div[php:functionString("' . static::class . '::hasClasses", @class, "' . $childDivClasses . '")]');

希望这有帮助!