首页 > 解决方案 > php - 在循环内创建新对象并执行函数

问题描述

我有一些问题,我尝试在 foreach 循环内对我的对象执行函数,但出现错误。在行中:$data_scrape->scrape($key); 在第二个 php 文件中,我尝试调用该函数,但我无法访问该函数。有什么方法可以解决问题?

我有一个调用 scrapeWeb.php 的类:

<?php
require ('vendor/autoload.php');

use Goutte\Client;
use GuzzleHttp\Client as GuzzleClient;
$test_array = array();

function scrape($value_to_search)
{

global $test_array;
$goutteClient = new Client();
$guzzleClient = new GuzzleClient(array(
    'timeout' => 60,
));
$goutteClient->setClient($guzzleClient);

$crawler = $goutteClient->request('GET', 
'someurl'.$value_to_search);


// Click on the "Security Advisories" link
$link = $crawler->selectLink('Article')->link();
$crawler = $goutteClient->click($link);


// Get the latest post in this category and display the titles
  $test = $crawler->filter('div > div > div > p')->each(function ($node) {
    # print ($node->text()) ."\n";

    global $test_array;
    array_push($test_array, $node->text());
});

return $test_array;

}

?>

以及更多调用 terms.php 的类:

<?php include 'scrapeTest.php'?>
<?php

foreach ($top_words as $key => $value) {
   $data_scrape = new scrapeTest();
   **$data_scrape->scrape($key);** **<------ THIS LINE DON'T WORKING.**
   echo "<p>{$key}-</p>";
  }
?>

标签: php

解决方案


推荐阅读