脚本中的foreach(),array_walk()

时间:2016-11-27 14:44:29

标签: php arrays

我已经努力在我的centos服务器中使用这个脚本,但是当我尝试执行它时,我有这个问题行。

[27-Nov-2016 14:37:15 UTC] PHP Warning:  array_walk() expects parameter 1 to be array, boolean given in /root/facebook-live-reactions/src/Facebook.php on line 48
[27-Nov-2016 14:37:15 UTC] PHP Warning:  Invalid argument supplied for foreach() in /root/facebook-live-reactions/src/Renderer.php on line 22

这是他有问题的文件>> Renderer.php

<?php

namespace FBReactions;

class Renderer {

    private $image;
    private $settings;

    public function __construct($image, $settings) {
        $this->image = $image;
        $this->settings = $settings;
    }

    /*
     * Adds reaction counts to an image
     */
    public function drawReactionCount($reactions)
    {
        $reactionSettings = $this->settings['REACTION_SETTINGS'];

        foreach ($reactions as $index => $reactionCount)
        {
            $this->image->text(
                $reactionCount,
                $reactionSettings['REACTIONS'][$index]['XPOS'],
                $reactionSettings['REACTIONS'][$index]['YPOS'],
                function ($font) use ($reactionSettings) {
                    $font->file($reactionSettings['FONT']['FAMILY']);
                    $font->size($reactionSettings['FONT']['SIZE']);
                    $font->color($reactionSettings['FONT']['COLOR']);
                    $font->align('center');
                }
            );
        }
    }

    /*
     * Draws the shoutout text to image
     */
    public function drawShoutout($user, $shoutout)
    {
        $shout = "@{$user}, {$shoutout}";
        $this->image->text(
            $shout,
            $this->settings['SHOUTOUT_SETTINGS']['SHOUTOUT_TEXT']['XPOS'],
            $this->settings['SHOUTOUT_SETTINGS']['SHOUTOUT_TEXT']['YPOS'],
            function ($font) {
                $font->file($this->settings['SHOUTOUT_SETTINGS']['SHOUTOUT_TEXT']['FONT']['FAMILY']);
                $font->size($this->settings['SHOUTOUT_SETTINGS']['SHOUTOUT_TEXT']['FONT']['SIZE']);
                $font->color($this->settings['SHOUTOUT_SETTINGS']['SHOUTOUT_TEXT']['FONT']['COLOR']);
                $font->align('left');
            }
        );
    }

    /*
     * Draws the profile image onto the shoutbox
     */
    public function drawProfileImage($filename, $xpos, $ypos) 
    {
        $this->image->insert($filename, 'bottom-left', $xpos, $ypos);
    }

    /*
     * I don't like this! Might be better to return $this->image instead
     */
    public function save($filename)
    {
        return $this->image->save($filename);
    }
}

Facebook.php代码:

<?php

namespace FBReactions;

class Facebook {

    private $fb;
    private $accessToken;

    public function __construct($fb, $accessToken) {

        $this->fb = $fb;
        $this->accessToken = $accessToken;
    }

    public function reactionCount($objectID, $reactions) {

        foreach ($reactions as $key => $position) {
            $fields[] = "reactions.type({$key}).limit(0).summary(total_count).as({$key})";
        }

        $reactionParams = ['ids' => $objectID, 'fields' => join(',', $fields)];
        $endpoint = '/?' . http_build_query($reactionParams);
        $request = $this->fb->request('GET', $endpoint, [], $this->accessToken);

        /*
         * Fetch the reactions count from Facebook
         */
        try {
            $response = $this->fb->getClient()->sendRequest($request);
            $reactions = json_decode($response->getBody(), true);
            $reactions = current($reactions);
        } catch (\Exception $e) {
            // die('Error getting reactions: ' . $e);
            return [];
        }

        /*
         * We don't need the id element. Remove it.
         */
        unset($reactions['id']);

        /*
         * We're only interested in the reaction count
         */
        array_walk($reactions, function(&$reaction) {
            $reaction = $reaction['summary']['total_count'];
        });

        return $reactions;
    }

    /*
     * Fetch latest comments
     */
    function comments($objectID)
    {
        $commentParams = ['filter' => 'stream', 'order' => 'reverse_chronological'];
        $request = $this->fb->request(
            'GET',
            "/{$objectID}/comments?" . http_build_query($commentParams),
            [],
            $this->accessToken
        );

        try {
            $response = $this->fb->getClient()->sendRequest($request);
            return json_decode($response->getBody(), true)['data'];
        } catch (\Exception $e) {
            // die('Error getting comments: ' . $e);
            return [];
        }
    }

    /*
     * Returns an array of comments that contains a specific keyword
     */
    function commentsByKeyword($objectID, $keyword, $caseSensitive = false)
    {
        $comments = $this->comments($objectID);

        return array_filter($comments, function ($comment) use ($keyword, $caseSensitive) {

            $message = $comment['message'];

            if ($caseSensitive) {
                return strpos($message, $keyword) > -1;
            } else {
                return strpos(strtolower($message), strtolower($keyword)) > -1;
            }
        });

    }

    /*
     * Downloads and saves public profile image
     */
    function saveProfileImage($uid, $width, $height, $filename)
    {
        $profileImageParams = [
            'width' => $width,
            'height' => $height,
        ];

        $endpoint = "http://graph.facebook.com/{$uid}/picture?" . http_build_query($profileImageParams);
        copy($endpoint, $filename);
    }
}

我很感激,如果有人在这种情况下帮助我,我在没有任何解决方案的情况下工作两天,我尝试了许多方法来修复它.. 谢谢

1 个答案:

答案 0 :(得分:0)

你在Facebook.php第32行:{/ p>进行current

$reactions = current($reactions);

当你得到布尔值而不是数组时,它可能就是这个地方:如果$reactions数组为空current()方法将返回false,你会收到警告&#39; array_walk()参数1为数组,布尔值为&#39;。要修复它,在执行current()之前,您应该检查$reactions数组是否为空