Andrew
Andrew

Reputation: 969

How to POST via Reddit API (addcomment)

I've been able to successfully log a user in and return their details. The next step is to get them to post a comment via my app.

I tried modifying code from the reddit-php-sdk -- https://github.com/jcleblanc/reddit-php-sdk/blob/master/reddit.php -- but I can't get it to work.

My code is as follows:

function addComment($name, $text, $token){
    $response = null;
    if ($name && $text){
        $urlComment = "https://ssl.reddit.com/api/comment";
        $postData = sprintf("thing_id=%s&text=%s",
                            $name,
                            $text);
        $response = runCurl($urlComment, $token, $postData);
    }
    return $response;
}

function runCurl($url, $token, $postVals = null, $headers = null, $auth = false){
    $ch = curl_init($url);
    $auth_mode = 'oauth';

    $options = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CONNECTTIMEOUT => 5,
        CURLOPT_TIMEOUT => 10
    );

    $headers = array("Authorization: Bearer {$token}");
    $options[CURLOPT_HEADER] = false;
    $options[CURLINFO_HEADER_OUT] = false;
    $options[CURLOPT_HTTPHEADER] = $headers;

    if (!empty($_SERVER['HTTP_USER_AGENT'])){
        $options[CURLOPT_USERAGENT] = $_SERVER['HTTP_USER_AGENT'];
    }

    if ($postVals != null){
        $options[CURLOPT_POSTFIELDS] = $postVals;
        $options[CURLOPT_CUSTOMREQUEST] = "POST";
    }

    curl_setopt_array($ch, $options);
    $apiResponse = curl_exec($ch);
    $response = json_decode($apiResponse);

    //check if non-valid JSON is returned
    if ($error = json_last_error()){
        $response = $apiResponse;    
    }
    curl_close($ch);

    return $response;
}

$thing_id = 't2_'; // Not the actual thing id
$perma_id = '2daoej'; // Not the actual perma id

$name = $thing_id . $perma_id;
$text = "test text";

$reddit_access_token = $_SESSION['reddit_access_token'] // This is set after login

addComment($name, $text, $reddit_access_token);

The addComment function puts the comment together according to their API -- http://www.reddit.com/dev/api

addComment then calls runCurl to make the request. My guess is that the curl request is messed up because I'm not receiving any response whatsoever. I'm not getting any errors so I'm not sure what's going wrong. Any help would really be appreciated. Thanks!

Upvotes: 1

Views: 1639

Answers (1)

Scopey
Scopey

Reputation: 6319

If you are using your own oAuth solution, I would suggest using the SDK as I said in my comment, but extend it to overwrite the construct method.

class MyReddit extends reddit {
    public function __construct()
    {
        //set API endpoint
        $this->apiHost = ENDPOINT_OAUTH;
    }
    public function setAuthVars($accessToken, $tokenType)
    {
        $this->access_token = $accessToken;
        $this->token_type = $tokenType;

        //set auth mode for requests
        $this->auth_mode = 'oauth';
    }
}

You just need to make sure that you call setAuthVars before running any api calls.

Upvotes: 0

Related Questions