Sunita Parab
Sunita Parab

Reputation: 127

How to get and store Twitter share counts in database?

I want to get twitter counts of my website and article pages. After getting them I want to store them in database. I tried getting counts and posting them on my query.php using ajax but its showing me Cross Origin Block Error. Is there any way to do this? or am I missing something?

$.ajax({
    url: "https://cdn.api.twitter.com/1/urls/count.json?url=<?php the_permalink(); ?>",
    crossDomain: true,
    crossOrigin: true,
    success: function(data) {
                   var count = JSON.parse(data);
                   $("#dd").html(count);
                   $.ajax
                       ({
                       type: "POST",
                       url: "post.php",
                       data: { stats: count, paralink:'<?php echo $rows['link_id'];?>', social:'2' },
                       success: function(data) {

                       },
                       dataType: 'json'
                       });
            }
    });

Upvotes: 1

Views: 142

Answers (1)

jeroen
jeroen

Reputation: 91734

It would probably be more efficient / faster if you just made one ajax call to your server and a request to twitter directly from your php script.

And it would solve your problem as well:

// you should validate the input, but for simplicity:
$link = $_POST['paralink'];
//              ^^^^^^^^ I am not sure if this is the page you want, you need to check that.

// get twitter json
$json = file_get_contents('https://cdn.api.twitter.com/1/urls/count.json?url=' . $link);

Now you just need the inner ajax call.

Upvotes: 1

Related Questions