Search code examples
phparraysapiredditstdclass

Post a link and add a comment on reddit with php


I would like to create a cronjob that regularly posts links to reddit (images on imgur) and adds a comment to the posted link.

I found something like this https://github.com/jcleblanc/reddit-php-sdk

and I created this script which publishes the links on reddit

<?
    require_once('reddit/reddit.php');
    $reddit = new reddit();
    
        $response = $reddit->createStory('title', 'https://i.imgur.com/....', 'funny');
    
    $pageInfo = $reddit->getPageInfo("https://i.imgur.com/...");
    
    
    
    echo var_dump($response);
    echo '<br><br>-------------------------------------------------------------------<br><br>';
    echo var_dump($pageInfo);
    
    echo '<br><br>-------------------------------------------------------------------<br><br>';
    
    echo 'name: '.$pageInfo->name;
    
    $response = $reddit->addComment('name from pageInfo', 'comment');
    ?>

Links work, but I can't get name from $pageInfo, I'm missing something, but I am really lost and confused...

$pageInfo var_dump, cuted bacause too long...

object(stdClass)#2 (2) {
  ["kind"]=>
  string(7) "Listing"
  ["data"]=>
  object(stdClass)#3 (5) {
    ["modhash"]=>
    NULL
    ["dist"]=>
    int(1)
    ["children"]=>
    array(1) {
      [0]=>
      object(stdClass)#4 (2) {
        ["kind"]=>
        string(2) "t3"
        ["data"]=>
        object(stdClass)#5 (108) {
          ["approved_at_utc"]=>
          NULL
          ["subreddit"]=>
          string(15) "funny"
          ["selftext"]=>
          string(0) ""
          ["author_fullname"]=>
          string(8) "t2_mgufp"
          ["saved"]=>
          bool(false)
          ["mod_reason_title"]=>
          NULL
          ["gilded"]=>
          int(0)
          ["clicked"]=>
          bool(false)
          ["title"]=>
          string(11) "test title3"
          ["link_flair_richtext"]=>
          array(0) {
          }
          ["subreddit_name_prefixed"]=>
          string(17) "r/funny"
          ["hidden"]=>
          bool(false)
          ["pwls"]=>
          NULL
          ["link_flair_css_class"]=>
          NULL
          ["downs"]=>
          int(0)
          ["thumbnail_height"]=>
          int(140)
          ["top_awarded_type"]=>
          NULL
          ["hide_score"]=>
          bool(false)
          ["name"]=>
          string(9) "t3_hjgx8u"

--------------------------update---------------------

Maybe I found... maybe it's not the easiest or most elegant way, but it seems to work...

This function convert the object(stdClass) into an array that php can read

function objectToArray( $object )
    {
        if( !is_object( $object ) && !is_array( $object ) )
        {
            return $object;
        }
        if( is_object( $object ) )
        {
            $object = get_object_vars( $object );
        }
        return array_map( 'objectToArray', $object );
    }

And with this, you can have the name parameter for add a comment

$array_pageInfo = objectToArray($pageInfo);
$comment_name = $array_pageInfo['data']['children'][0]['data']['name'];

But now I have one last big doubt. In the various tests I noticed that the authorization token expires every hour, I would like to create a cronjob on my server, but if the token expires...

How do I make this thing work in a cronjob?


Solution

  • In the end, I kept working on it these days, and I think I solved it.

    It was not easy or quick, and I also had to add other things like a small script that renews the token as suggested and the imgur api to upload a picture, because many subreddits do not accept other addresses such as sources, etc. etc. etc.

    A complete script made up of small parts came out, and in the end it was easier to write it than to understand how each step works.

    However, a completely new script came out, independent of the sdk initially used, which does everything by itself, and it seems to do it well on crontab too.

    And since I have a memory that sucks, I explained it step by step in my blog, for the next times that I will need it, and while I was there, I also translated it into English.

    If anyone needs it, you can find it all here: https://www.alebalweb-blog.com/87-cronjob-that-uploads-a-photo-from-imgur-and-adds-a-comment-on-reddit.html