下面是我的脚本,显示错误$csvfileoutput
是未定义的变量。正如我已经定义了这个变量。
define('CSV_PATH','/var/www/Scripts/csvfiles/');
$csv_fileoutput = CSV_PATH . "top_selling_Category_output_.csv";
$csvfileoutput = fopen($csv_fileoutput, 'w');
fputcsv($csvfileoutput, array('Part URL'));
$website= "https://www.website.com";
function scrapurls($val, $tagname, $attribute,$value){
$html = file_get_contents($val);
$dom = new DOMDocument();
$dom->loadHTML($html);
$links = array();
$equivalentparts = array();
$divs = $dom->getElementsByTagName($tagname);
foreach($divs as $div) {
$cl = $div->getAttribute($attribute);
if ($cl == $value) {
//echo "\nID: ".$cl;
$hrefs = $div->getElementsByTagName("a");
foreach ($hrefs as $href){
$links[] = $href->getAttribute("href");
$equivalentparts[] = $href->nodeValue;
}
}
}
array_walk($links, function (&$value, $key) { $value="https://www.website.com$value"; });
return(array_unique($links));
}
$urls = array();
$urls = array('https://www.website.com/topsellers/?vertical=3');
foreach ($urls as $val){
$pageinateurl = array($val);
findpagesurls($val);
}
function findpagesurls($val){
$pageinateurl = array();
$pageinateurl = scrapurls($val, "div","style", "display: inline-block;");
echo "\nPage 1: Total Urls - ".count($pageinateurl) . "\n\n";
foreach ($pageinateurl as $produrl){
scrapdata($produrl);
}
}
function scrapdata($producturl) {
$producturl = trim($producturl);
echo $producturl . "\n";
fputcsv($csvfileoutput, array($producturl));
}
?>
答案 0 :(得分:0)
在函数scrapdata
中,未定义此变量。阅读变量范围。
http://php.net/manual/en/language.variables.scope.php
最好的解决方案是将此变量作为参数
发送到此函数答案 1 :(得分:0)
查看变量http://php.net/manual/en/language.variables.scope.php
的范围<?php $a = 1; /* global scope */ function test() { echo $a; /* reference to local scope variable */ } test(); ?>
答案 2 :(得分:0)
HotFix,但你不应该这样做。在函数内部访问全局变量并不好。
function scrapdata($producturl) {
global $csvfileoutput;
$producturl = trim($producturl);
echo $producturl . "\n";
fputcsv($csvfileoutput, array($producturl));
}
应该是。
function scrapdata($producturl,$csvfileoutput) {
$producturl = trim($producturl);
echo $producturl . "\n";
fputcsv($csvfileoutput, array($producturl));
}