1

我正在尝试在 Laravel 5.4 中使用 cuab 的 PHPCrawl,并使用此包通过作曲家将其包含在内: https ://packagist.org/packages/mmerian/phpcrawl

我试过运行这个示例代码:

class MyCrawler extends PHPCrawler  
{ 
  function handleDocumentInfo($DocInfo)  
  { 
    if (PHP_SAPI == "cli"){
      $lb = "\n"; 
    } else {
      $lb = "<br />"; 
    }
    echo "Page requested: ".$DocInfo->url." (".$DocInfo->http_status_code.")".$lb; 
    echo "Referer-page: ".$DocInfo->referer_url.$lb; 

    if ($DocInfo->received == true) {
      echo "Content received: ".$DocInfo->bytes_received." bytes".$lb; 
    }
    else {
      echo "Content not received".$lb;  
    }

    echo $lb; 
    flush(); 
  }  
} 

$crawler = new MyCrawler(); 
$crawler->setURL("www.php.net"); 
$crawler->addContentTypeReceiveRule("#text/html#"); 
$crawler->addURLFilterRule("#\.(jpg|jpeg|gif|png)$# i"); 
$crawler->enableCookieHandling(true);  
$crawler->setTrafficLimit(1000 * 1024); 
$crawler->go(); 
$report = $crawler->getProcessReport(); 

if (PHP_SAPI == "cli") {
  $lb = "\n"; 
} else {
  $lb = "<br />"; 
}

echo "Summary:".$lb; 
echo "Links followed: ".$report->links_followed.$lb; 
echo "Documents received: ".$report->files_received.$lb; 
echo "Bytes received: ".$report->bytes_received." bytes".$lb; 
echo "Process runtime: ".$report->process_runtime." sec".$lb; 

但它会引发多个这样的错误:

找不到类“App\Http\Controllers\PHPCrawler”

您将如何引用正确的命名空间以便我可以在 Laravel 中使用该脚本?

4

1 回答 1

2

您应该通过 composer.json 文件中的类映射添加文件。您可以在他们的github 页面上看到类映射的示例,在撰写本文时看起来像这样:

"autoload": {
    "classmap": [
        "libs/Utils/PHPCrawlerUtils.class.php",
        "libs"
    ]
}

您必须在文件vendor/mmerian/phpcrawl前面加上 libs 文件夹的路径或路径。

于 2017-03-16T16:21:54.600 回答