为了账号安全,请及时绑定邮箱和手机立即绑定

Goutte - array_push() 期望参数 1 是数组,给定 null

Goutte - array_push() 期望参数 1 是数组,给定 null

PHP
慕田峪7331174 2022-07-16 11:08:59
我正在使用PHP 7.1.33和"fabpot/goutte": "^3.2"。运行以下脚本时,我得到以下信息:PHP Warning:  array_push() expects parameter 1 to be array, null given in C:\Users\testFile.php on line 88PHP Stack trace:PHP   1. {main}() C:\Users\testFile.php:0PHP   2. updateCalendarDetailsData() C:\Users\testFile.php:104PHP   3. Symfony\Component\DomCrawler\Crawler->each($closure = *uninitialized*) C:\Users\testFile.php:99PHP   4. {closure:C:\Users\testFile.php:31-99}($node = *uninitialized*, *uninitialized*) C:\Users\Desktop\Code\vendor\symfony\dom-crawler\Crawler.php:368PHP   5. Symfony\Component\DomCrawler\Crawler->each($closure = *uninitialized*) C:\Users\testFile.php:90PHP   6. {closure:C:\Users\testFile.php:53-90}($LEFT_TD = *uninitialized*, *uninitialized*) C:\Users\Desktop\Code\vendor\symfony\dom-crawler\Crawler.php:368PHP   7. array_push(*uninitialized*, *uninitialized*) C:\Users\testFile.php:88我正在运行以下脚本:<?phprequire 'vendor/autoload.php';use Goutte\Client;use Symfony\Component\DomCrawler\Crawler;function updateCalendarDetailsData(){    $client = new Client();    $x = 1;    $LIMIT = 3;    global $x;    global $LIMIT;    $x++;    $res1Array = array();    $ffUrlArr = ["https://www.forexfactory.com/calendar.php?month=Jan2020"];    foreach ($ffUrlArr as $key => $v) {        try {            $crawler = $client->request('GET', $ffUrlArr[$key]);        } catch (\Exception $ex) {            error_log($ex);        }        $TEMP = array();        $count = $crawler->filter('.calendar_row')->count();        $i = 1; // count starts at 1        $crawler->filter('.calendar_row')->each(function ($node) use ($count, $i) {            $EVENT = array();            global $res1Array;            $EVENTID = $node->attr('data-eventid');有什么建议为什么我会收到警告?感谢您的回复!
查看完整描述

1 回答

?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

你需要global $res1Array在updateCalendarDetailsData()函数的顶部。匿名回调函数正在尝试使用此全局变量,但您正在初始化局部变量,而不是全局变量。您似乎假设它global只是允许您从任何外部范围访问变量,但它仅适用于全局变量。


$TEMP和有类似的问题$EVENT。


更好的方法是将变量包含在use()函数列表中。由于您正在使用 修改变量array_push(),因此您需要使用 将它们声明为引用&。


<?php

function updateCalendarDetailsData()

{

    $client = new Client();


    $x = 1;

    $LIMIT = 3;

    global $x;

    global $LIMIT;

    $x++;

    $res1Array = array();


    $ffUrlArr = ["https://www.forexfactory.com/calendar.php?month=Jan2020"];

    foreach ($ffUrlArr as $key => $v) {


        try {

            $crawler = $client->request('GET', $ffUrlArr[$key]);

        } catch (\Exception $ex) {

            error_log($ex);

        }


        $TEMP = array();


        $count = $crawler->filter('.calendar_row')->count();

        $i = 1; // count starts at 1

        $crawler->filter('.calendar_row')->each(function ($node) use ($count, $i, &$res1Array) {

            $EVENT = array();


            $EVENTID = $node->attr('data-eventid');


            $API_RESPONSE = file_get_contents('https://www.forexfactory.com/flex.php?do=ajax&contentType=Content&flex=calendar_mainCal&details=' . $EVENTID);


            $API_RESPONSE = str_replace("<![CDATA[", "", $API_RESPONSE);

            $API_RESPONSE = str_replace("]]>", "", $API_RESPONSE);


            $html = <<<HTML

<!DOCTYPE html>

<html>

    <body>

       $API_RESPONSE

    </body>

</html>

HTML;


            $subcrawler = new Crawler($html);


            $subcrawler->filter('.calendarspecs__spec')->each(function ($LEFT_TD) use (&$res1Array, &$TEMP, &$EVENT) {


                $LEFT_TD_INNER_TEXT = trim($LEFT_TD->text());


                if ($LEFT_TD_INNER_TEXT == "Source") {


                    $TEMP = array();

                    $LEFT_TD->nextAll()->filter('a')->each(function ($LINK) use (&$TEMP) {

                        array_push($TEMP, $LINK->text(), $LINK->attr('href'));

                    });


                    $EVENT['sourceTEXT'] = $TEMP[0];

                    $EVENT['sourceURL'] = $TEMP[1];

                    $EVENT['latestURL'] = $TEMP[3];

                }


                if ($LEFT_TD_INNER_TEXT == "Measures") {

                    $EVENT['measures'] = $LEFT_TD->nextAll()->text();

                }


                if ($LEFT_TD_INNER_TEXT == "Usual Effect") {

                    $EVENT['usual_effect'] = $LEFT_TD->nextAll()->text();

                }


                if ($LEFT_TD_INNER_TEXT == "Derived Via") {

                    $EVENT['derived_via'] = $LEFT_TD->nextAll()->text();

                    var_dump($EVENT);

                    print_r($EVENT);

                    if (empty($EVENT)) {

                        echo "test";

                    }

                    array_push($res1Array, $EVENT); // <---- HERE I GET THE ERROR!

                }

            });

            $i++;

            if ($i > $count) {

                echo "<pre>";

                var_dump($res1Array);

                print_r($res1Array);

                echo "</pre>";

                exit;

            }

        });

    }

    return $res1Array;

}


查看完整回答
反对 回复 2022-07-16
  • 1 回答
  • 0 关注
  • 227 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号