php - 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>";
}
?>
解决方案
推荐阅读
- python - 未知长度字符串的 C++ 数组,其行为类似于 Python 字符串列表
- c++ - 潜在的数据竞赛是数据竞赛吗?
- r - 范围值和 geom_bar 的问题
- amcharts4 - 设置 AmChart4 XYCursor 的 X 和 Y 标签的字体大小和颜色
- android - 除了具有更高优先级内存需求的应用程序之外,还有什么可以破坏系统的活动?
- sql-server - 在 SQL Server 2008 R2 中将 Excel 指数格式转换回其文本
- java - 我应该使用什么版本的 Java 来开发我的 MOJO Maven 插件?
- android - PopupWindow内的RecyclerView,单击项目后如何关闭?
- kubernetes - GRPC::Unavailable: 14:DNS 解析失败,当尝试从 ruby 客户端连接 python 服务器时都在 kubernates 上提供服务
- node.js - 关于在 node js 中使用 sinon 进行单元测试