HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Recent topics feed issue with latest release

edited July 2017 in Feedback

I recently upgraded the forum to 2.3.1 (+ to PHP7) and now have issues with the code quoted below.

This code grabs the latest discussions, caches them and displays on a non-forum page but now I have over a thousand exception errors in bugsnag for it, "HTTP request failed! HTTP/1.1 403 Forbidden". See screenshot.

Please can someone help identify the issue? Here's the full function:

public function getLatestDiscussions($limit)    
{
        $discussions = [];
        $displayMaxSub = 1;
        $vanillaUrl = url('forum', [], true);
        $jsonStr = file_get_contents($vanillaUrl . '/discussions.json', false, stream_context_create([
            'ssl' => [
                "verify_peer"=> false,
                "verify_peer_name"=> false,
            ],
        ]));

        if (!$jsonStr)
        {
            return [];
        }

        $json = json_decode($jsonStr, true);
        $i = 0;

        $discussions_count = count($json['Discussions']);

        foreach ($json['Discussions'] as $discussion)
        {
            if ($i >= $limit) break;

            $discussions[] = [
                'id'             => $discussion['DiscussionID'],
                'subject'        => $discussion['Name'],
                'body'           => $discussion['Body'],
                'username'       => $discussion['LastName'],
                'date'           => $discussion['DateLastComment'],
                'link'           => url('forum/discussion/' . $discussion['DiscussionID']),
            ];

            if ($discussion['CountComments'] > 1)
            {
                $jsonStr_single = file_get_contents($vanillaUrl . '/discussion.json?DiscussionID=' . $discussion['DiscussionID']);

                if ($jsonStr_single)
                {
                    $json_single = json_decode($jsonStr_single, true);

                    if (isset($json_single['Comments']))
                    {
                        $addedComments = 1;

                        $comments = array_reverse($json_single['Comments']);
                        foreach ($comments as $comment)
                        {
                            if ($addedComments === $displayMaxSub || $i === $limit) break;

                            $discussions[] = [
                                'id'       => $discussion['DiscussionID'],
                                'subject'  => 'RE: ' . $discussion['Name'],
                                'body'     => $comment['Body'],
                                'username' => $comment['InsertName'],
                                'date'     => $comment['DateLastComment'],
                                'link'     => $this->generateCommentLink($comment['CommentID']),
                            ];

                            $addedComments++;
                            $i++;
                        }
                    }
                }
            }

            $i++;
        }

        return $discussions;
    }

Comments

  • rbrahmsonrbrahmson ✭✭✭

    I can't help you with this specific question, but I wonder about the approach that does directly to the data - why are you not using Vanilla's ability to create an RSS feed (possibly with the aid of the RSS Options plugin) and just grab the feed's result into whatever destination you needed?

  • I think at the time I preferred the flexibility of displaying and caching exactly how I liked rather than using third-party plugins. This was a couple of years ago so not sure how things have changed.

  • R_JR_J Admin

    Instead of using file_get_contents you should try curl.

Sign In or Register to comment.