title: How to POST via Reddit API (addcomment)
body: <p>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.</p>
  
  <p>I tried modifying code from the reddit-php-sdk -- <a href="https://github.com/jcleblanc/reddit-php-sdk/blob/master/reddit.php" rel="nofollow">https://github.com/jcleblanc/reddit-php-sdk/blob/master/reddit.php</a> -- but I can't get it to work.</p>
  
  <p>My code is as follows:</p>
  
  <pre><code>function addComment($name, $text, $token){
      $response = null;
      if ($name &amp;&amp; $text){
          $urlComment = "https://ssl.reddit.com/api/comment";
          $postData = sprintf("thing_id=%s&amp;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 =&gt; true,
          CURLOPT_CONNECTTIMEOUT =&gt; 5,
          CURLOPT_TIMEOUT =&gt; 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);
  </code></pre>
  
  <p>The addComment function puts the comment together according to their API -- <a href="http://www.reddit.com/dev/api" rel="nofollow">http://www.reddit.com/dev/api</a></p>
  
  <p>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!</p>