Jump to content

PHP and Twitter


chasethemetal

Recommended Posts

Hello!

 

I have a twitter button. But I would like to have a form button's action to activate the "tweet/share" link. Why? Because I want to record in mySQL database the fact that someone has in fact clicked the link to tweet. How I was thinking about doing... But really have no idea is like so.

 

<form action="tweet.php" method="POST">

<input type="button" name="tweet" value="ip address" />

</form>

 

then in the tweet.php

 

I would have php insert the gathered IP address (ive got this part working with a geocoder class) into the database as confirmation. Then the php code would execute the hyperlink / java which is this:

 

<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://mycontent.com" data-count="horizontal" data-via="person-to-tweet-at">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>

 

Activating the link as if someone where to click the link, but automatically.

 

Basically I am looking for a way to record who clicks the Twitter button. Any guidance on this topic would be much appreciated.

Link to comment
Share on other sites

I would go for another approach.

 

First, make the link to twitter go into a social.php page like this:

<a href="social.php?p=twitter">Follow me on Twitter</a>

 

Then make social.php

<?php
$social_sites = array('twitter', 'facebook', 'linkedin');
$site = $_GET['p'];
if (!in_array($_GET['p'], $social_sites)) { die('What were you thinking?'); }

$ip = $_SERVER['REMOTE_ADDR']; //or whatever u use to get the visitor's IP

$results = mysql_query("INSERT INTO social_clicks (site, ip) VALUES ('$site', '$ip')");

$url = "http://www.$site.com";
header("Location: $url");
?>

 

The script is very simple. Based on the "p" url parameter (passed by GET) it specifies what kind of social media the user wants to visit. It could be social.php?p=facebook for Facebook and so on. If the social media is correct (in the white list), you insert a row in the database with the right social media and IP, and finally redirect him to the correct url. It may certainly need some extra code, but it should give the general idea.

 

I assumed you needed a way to handle more than one social media (for scalability's sake), but if you want just Twitter, it can be easily modified.

Link to comment
Share on other sites

Thats a great idea. However I am really stumped because I know you can have php go to a url. But if you look at the <a href> tag there is all this crucial information inside it. It would be nice is for instance if you could do this with PHP

 

$url = ' "http://twitter.com/share" class="twitter-share-button" data-url="http://mycontent.com" data-count="horizontal" data-via="person-to-tweet-at" '

 

I just can't seem to figure out a way for all of that class, and data tags to be automatically associated with the twitter link...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.