0

I am trying to create a randomly generated phrase that can easily be shared amongst social media websites, specifically twitter. I am using the following PHP code to generate a random phrase.

This code looks in 'responses.txt' for a line with a phrase and I can call that line.

<!-- HEADER -->
<?php
$randomThings = file('**responses.txt**', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
?>

<!-- CALL SCRIPT -->
<?php
echo $randomThings[mt_rand(0,count($randomThings)-1)];
?>

How would I be able to have, for example, retweet button next to this generated line that retweets the phrase with a predetermined #hatchtag (via #[websitename]).

I'm more interested in the twitter aspect, but other social media websites could help other people.

Rogan
  • 3
  • 1

1 Answers1

0

The re-tweet intent itself has problems right now. It's been filed as a bug since November so I don't think you'll want to use the re-tweet functionality from an external website. You can simulate a re-tweet with a regular tweet intent and pre-filling in the text, (which sounds like what you actually want to do). With the tweet intent you send a HTTP request to https://twitter.com/intent/tweet. You could then include the text parameter to pre-fill the text when the HTTP request is sent, or the link is clicked.

Using your example it would look something like this:

<!-- HEADER -->
    <?php
    $randomThings = file('**responses.txt**', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    ?>

    <!-- CALL SCRIPT -->
    <?php
    $newThings = $randomThings[mt_rand(0,count($randomThings)-1)]; //must evaluate to a string
    echo $newThings;
    echo '<a href="https://twitter.com/intent/tweet?text='.$newThings.'">Link text</a>';
    ?>

this would be an unstyled link instead of a "button" but you can adapt it to be a button using standard HTML/CSS styling.


ref: https://dev.twitter.com/docs/intents

Chamilyan
  • 9,347
  • 10
  • 38
  • 67
  • http://www.overmined.com - as an example, the tweet intent doesn't seem to be able to pull the variable. It's left blank. `'; ?>` – Rogan Dec 22 '11 at 05:52
  • ok I see what the problem is, updated the answer. Quotes and you need to first create the variable before echoing it out, and you would use the same variable in the share. By the way, are you creating the $randromThings variable elsewhere? `$randomThings[mt_rand(0,count($randomThings)-1)];` You need to make sure this evaluates to a string. – Chamilyan Dec 22 '11 at 07:07
  • If I use `'.$newThings.'` then the button doesn't appear altogether. If I use double quotations, it does - but there isn't any text that is imported. – Rogan Dec 22 '11 at 07:31
  • OH! Nevermind, you forgot a quotation. The code should be as follows: `Link text'; ?>` – Rogan Dec 22 '11 at 07:44