PHP class for DaniWeb API's unauthenticated calls.

pritaeas 2 Tallied Votes 426 Views Share

Here's my base class and class for the unauthenticated DaniWeb API calls. Not every method has all features implemented yet, and it's not been fully tested either. Here's how to start using it:

include 'DwApiBase.class.php';
include 'DwApiOpen.class.php';

$dwapi = new DwApiOpen();

$result = $dwapi->GetMemberActivityPoints(94719);
$result = $dwapi->GetMemberEndorsements(94719);
$result = $dwapi->GetMemberReputationComments(94719);
$result = $dwapi->SearchArticles('daniweb-api');
$result = $dwapi->SearchMembers('pritaeas');

Open to suggestions, questions, etc.

diafol commented: WOW! +0
LastMitch commented: Nice! +11
// DwApiBase.class.php
<?php
class DwApiBase {
    /**
     * @var array List of supported article types.
     */
    protected $articleTypes = array
    (
        'unanswered', 'solved', 'threads', 'news', 'reviews',
        'interviews', 'tutorials', 'code', 'whitepapers'
    );

    /**
     * @var array List of supported post types.
     */
    protected $postTypes = array
    (
        'solved', 'upvoted', 'downvoted'
    );

    /**
     * @var array List of forum relation types.
     */
    protected $relationTypes = array
    (
        'ancestors', 'children', 'descendants'
    );
    
    /**
     * Get the list of supported article types.
     *
     * @param bool $sorted Sort alphabetically (optional), default true.
     * @return array List of article types.
     */
    public function GetArticleTypes($sorted = true)
    {
        $result = $this->articleTypes;
        if ($sorted)
        {
            sort($result);
        }
        return $result;
    }

    /**
     * Get the list of supported post types.
     *
     * @param bool $sorted Sort alphabetically (optional), default true.
     * @return array List of post types.
     */
    public function GetPostTypes($sorted = true)
    {
        $result = $this->postTypes;
        if ($sorted)
        {
            sort($result);
        }
        return $result;
    }

    /**
     * Get the list of supported forum relation types.
     *
     * @param bool $sorted Sort alphabetically (optional), default true.
     * @return array List of forum relation types.
     */
    public function GetRelationTypes($sorted = true)
    {
        $result = $this->relationTypes;
        if ($sorted)
        {
            sort($result);
        }
        return $result;
    }

    /**
     * Get an URL's page contents as a string.
     *
     * @param string $url URL to get.
     * @return bool|string URL page contents, false on error.
     */
    protected function GetUrl($url)
    {
        $result = false;
        if (extension_loaded('curl'))
        {
            $ch = curl_init();
            if ($ch)
            {
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $curlResult = curl_exec($ch);
                if ($curlResult)
                {
                    $result = $curlResult;
                }
                curl_close($ch);
            }
        }
        else if (ini_get('allow_url_fopen'))
        {
            $result = file_get_contents($url);
        }
        return $result;
    }

    /**
     * Checks if the ID is a valid positive integer.
     *
     * @param mixed $id ID.
     * @return bool True if positive integer, false otherwise.
     */
    protected function IsValidId($id)
    {
        return is_int($id) and ($id > 0);
    }
}
?>



// DwApiOpen.class.php
<?php
class DwApiOpen extends DwApiBase
{
    /**
     * Get posts for a specific article.
     *
     * @param int $articleId Article ID.
     * @param int $page Page number (optional).
     * @return mixed JSON result, false on error.
     */
    public function GetArticlePosts($articleId, $page = null)
    {
        if (!$this->IsValidId($articleId))
        {
            return false;
        }

        $url = "http://www.daniweb.com/api/articles/{$articleId}/posts";
        if ($this->IsValidId($page))
        {
            $url .= "?page={$page}";
        }

        return $this->GetUrl($url);
    }

    /**
     * Get a list of (specific) articles.
     *
     * @param mixed $articleIds Article ID as int, or array of int (optional).
     * @param int|null $page Page number (optional).
     * @return bool|string JSON result, false on error.
     */
    public function GetArticles($articleIds = null, $page = null)
    {
        $url = "http://www.daniweb.com/api/articles";

        $articleIdList = array();
        if ($this->IsValidId($articleIds))
        {
            $articleIdList[] = $articleIds;
        }
        else if (is_array($articleIds))
        {
            foreach ($articleIds as $forumId)
            {
                if ($this->IsValidId($forumId))
                {
                    $articleIdList[] = $forumId;
                }
            }
        }

        if (count($articleIdList) > 0)
        {
            $articleIdString = implode(';', $articleIdList);
            $url .= "/{$articleIdString}";
        }

        if ($this->IsValidId($page))
        {
            $url .= "?page={$page}";
        }

        return $this->GetUrl($url);
    }

    /**
     * Get a list of articles for a specific forum ID, or an array of forum IDs.
     *
     * @param mixed $forumIds Forum ID as int, or array of int.
     * @param int|null $page Page number.
     * @return bool|string JSON result, false on error.
     */
    public function GetForumArticles($forumIds, $page = null)
    {
        $forumIdList = array();

        if ($this->IsValidId($forumIds))
        {
            $forumIdList[] = $forumIds;
        }
        else if (is_array($forumIds))
        {
            foreach ($forumIds as $forumId)
            {
                if ($this->IsValidId($forumId))
                {
                    $forumIdList[] = $forumId;
                }
            }
        }

        if (count($forumIdList) < 1)
        {
            return false;
        }

        $forumIdString = implode(';', $forumIdList);
        $url = "http://www.daniweb.com/api/forums/{$forumIdString}/articles";

        if ($this->IsValidId($page))
        {
            $url .= "?page={$page}";
        }

        return $this->GetUrl($url);
    }

    /**
     * Get posts for a specific forum.
     *
     * @param int $forumId Forum ID.
     * @param int|null $page Page number (optional).
     * @return bool|string JSON result, false on error.
     */
    public function GetForumPosts($forumId, $page = null)
    {
        if (!$this->IsValidId($forumId))
        {
            return false;
        }

        $url = "http://www.daniweb.com/api/forums/{$forumId}/posts";
        if ($this->IsValidId($page))
        {
            $url .= "?page={$page}";
        }

        return $this->GetUrl($url);
    }

    /**
     * Get a list of (filtered) forums.
     *
     * @param mixed $forumIds Forum ID as int, or array of int (optional).
     * @param null|string $relation Forum relation type (optional).
     * @param bool|null $includeSelf Include the forumID in the result (optional), default false.
     * @return bool|string JSON result, false on error.
     */
    public function GetForums($forumIds = null, $relation = null, $includeSelf = null)
    {
        $url = 'http://www.daniweb.com/api/forums';

        $forumIdList = array();
        if ($this->IsValidId($forumIds))
        {
            $forumIdList[] = $forumIds;
        }
        else if (is_array($forumIds))
        {
            foreach ($forumIds as $forumId)
            {
                if ($this->IsValidId($forumId))
                {
                    $forumIdList[] = $forumId;
                }
            }
        }

        if (count($forumIdList) > 0)
        {
            $url .= '/' . implode(';', $forumIdList);
        }

        if (is_string($relation) and in_array($relation, $this->relationTypes))
        {
            $url .= "/$relation";
        }

        if (is_bool($includeSelf))
        {
            $url .= '?' . ($includeSelf ? 'true' : 'false');
        }

        return $this->GetUrl($url);
    }

    /**
     * Get activities for a specific member.
     *
     * @param int $memberId Member ID.
     * @return bool|string JSON result, false on error.
     */
    public function GetMemberActivityPoints($memberId)
    {
        if (!$this->IsValidId($memberId))
        {
            return false;
        }

        return $this->GetUrl("http://www.daniweb.com/api/members/{$memberId}/activities");
    }

    /**
     * Get a list of articles for a specific member, or members.
     *
     * @param mixed $memberIds Member ID as int, or array of int.
     * @param int|null $page Page number (optional).
     * @return bool|string JSON result, false on error.
     */
    public function GetMemberArticles($memberIds, $page = null)
    {
        $memberIdList = array();
        if ($this->IsValidId($memberIds))
        {
            $memberIdList[] = $memberIds;
        }
        else if (is_array($memberIds))
        {
            foreach ($memberIds as $forumId)
            {
                if ($this->IsValidId($forumId))
                {
                    $memberIdList[] = $forumId;
                }
            }
        }

        if (count($memberIdList) < 1)
        {
            return false;
        }

        $memberIdString = implode(';', $memberIdList);
        $url = "http://www.daniweb.com/api/members/{$memberIdString}/articles";

        if ($this->IsValidId($page))
        {
            $url .= "?page={$page}";
        }

        return $this->GetUrl($url);
    }

    /**
     * Get endorsements for a specific member.
     *
     * @param int $memberId Member ID.
     * @return bool|string JSON result, false on error.
     */
    public function GetMemberEndorsements($memberId)
    {
        if (!$this->IsValidId($memberId))
        {
            return false;
        }

        return $this->GetUrl("http://www.daniweb.com/api/members/{$memberId}/endorsements");
    }

    /**
     * Get posts for a specific member ID, optionally filtered.
     *
     * @param int $memberId Member ID.
     * @param null|string $postType Post type to filter on (optional).
     * @param int|null $page Page number (optional).
     * @return bool|string JSON result, false on error.
     */
    public function GetMemberPosts($memberId, $postType = null, $page = null)
    {
        if (!$this->IsValidId($memberId))
        {
            return false;
        }

        $url = "http://www.daniweb.com/api/members/{$memberId}/posts";

        $filter = in_array($postType, $this->postTypes) ? $postType : '';
        if (!empty($filter))
        {
            $url .= "?filter={$filter}";
        }

        if ($this->IsValidId($page))
        {
            $url .= (empty($filter) ? '?' : '&') . "page={$page}";
        }

        return $this->GetUrl($url);
    }

    /**
     * Get reputation comments for a specific member.
     *
     * @param int $memberId Member ID.
     * @param int|null $page Page number (optional).
     * @return bool|string JSON result, false on error.
     */
    public function GetMemberReputationComments($memberId, $page = null)
    {
        if (!$this->IsValidId($memberId))
        {
            return false;
        }

        $url = "http://www.daniweb.com/api/members/{$memberId}/comments";

        if ($this->IsValidId($page))
        {
            $url .= "?page={$page}";
        }

        return $this->GetUrl($url);
    }

    /**
     * Get a list of members, or a list of specific members.
     *
     * @param mixed $members Member as string, member ID as int or array of int (optional).
     * @param int|null $page Page number (optional).
     * @return bool|string JSON result, false on error.
     */
    public function GetMembers($members, $page = null)
    {
        $url = 'http://www.daniweb.com/api/members';

        if (is_string($members))
        {
            $url .= "?username={$members}";
        }
        else
        {
            if ($this->IsValidId($members))
            {
                $url .= "/{$members}";
            }
            else if (is_array($members))
            {
                $memberList = array ();
                foreach ($members as $member)
                {
                    if ($this->IsValidId($member))
                    {
                        $memberList[] = $member;
                    }
                }
                $url .= implode(';', $memberList);
            }

            if ($this->IsValidId($page))
            {
                $url .= "?page={$page}";
            }
        }

        return $this->GetUrl($url);
    }

    /**
     * Get reputation comments for a specific post.
     *
     * @param int $postId Post ID.
     * @param int|null $page Page number (optional).
     * @return bool|string JSON result, false on error.
     */
    public function GetPostReputationComments($postId, $page = null)
    {
        if (!$this->IsValidId($postId))
        {
            return false;
        }

        $url = "http://www.daniweb.com/api/posts/{$postId}/comments";

        if ($this->IsValidId($page))
        {
            $url .= "?page={$page}";
        }

        return $this->GetUrl($url);
    }

    /**
     * Get a list of posts, or a list of specific posts.
     *
     * @param mixed $postIds Post ID as int, or array of int.
     * @param int|null $page Page number (optional).
     * @return bool|string JSON result, false on error.
     */
    public function GetPosts($postIds = null, $page = null)
    {
        $url = "http://www.daniweb.com/api/posts";

        if ($this->IsValidId($postIds))
        {
            $url .= "/{$postIds}";
        }
        else if (is_array($postIds))
        {
            $postIdList = array();

            foreach ($postIds as $postId)
            {
                if ($this->IsValidId($postId))
                {
                    $postIdList[] = $postId;
                }
            }

            if (count($postIdList) > 0)
            {
                $url .= '/' . implode(';', $postIdList);
            }
        }

        if ($this->IsValidId($page))
        {
            $url .= "?page={$page}";
        }

        return $this->GetUrl($url);
    }

    /**
     * Searches articles for the given query.
     *
     * @param string $query Search query.
     * @param int|null $page Page number (optional).
     * @return bool|string JSON result, false on error.
     */
    public function SearchArticles($query, $page = null)
    {
        if (empty($query) or !is_string($query))
        {
            return false;
        }

        $url = 'http://www.daniweb.com/api/articles/search?query=' . urlencode($query);

        if ($this->IsValidId($page))
        {
            $url .= "&page={$page}";
        }

        return $this->GetUrl($url);
    }

    /**
     * Searches members for the given member name.
     *
     * @param string $memberName Member name to search.
     * @param int|null $page Page number (optional).
     * @return bool|string JSON result, false on error.
     */
    public function SearchMembers($memberName, $page = null)
    {
        if (empty($memberName) or !is_string($memberName))
        {
            return false;
        }

        $url = 'http://www.daniweb.com/api/members/search?query=' . urlencode($memberName);

        if ($this->IsValidId($page))
        {
            $url .= "&page={$page}";
        }

        return $this->GetUrl($url);
    }
}
?>
Member Avatar for diafol
diafol

Really nice pritaeas. Puts mine to shame :)

This should certainly encourage members to enter the api contest.

Member Avatar for LastMitch
LastMitch

Really nice pritaeas. Puts mine to shame :)

I think both of you guys did a utmost code snippets in a short amounts of time. I don't recalled that much code snippets done in a week. Thanks. =)

This should certainly encourage members to enter the api contest.

I hope it will. I hope more PHP members can try this and post some code of what they done. This something very new.

pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

Thanks. Updates to my classes will be on GitHub (see my signature). Just refactored a bit, reducing the number of lines of code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.