Echo Twitter share count with PHP? - php

I am trying to create some text based share buttons for my Wordpress that also output the shared amount. So far I got it working with Facebook and Delicious but I am not sure how to get it going with Twitter.
This is what I did for Delicious.
<?php
$shareUrl = urlencode(get_permalink($post->ID));
$shareTitle = urlencode($post->post_title);
$deliciousStats = json_decode(file_get_contents('http://feeds.delicious.com/v2/json/urlinfo/data?url='.$shareUrl));
?>
<a onclick='window.open("http://delicious.com/save?v=5&noui&jump=close&url=<?php echo $shareUrl; ?>&title=<?php echo $shareTitle; ?>", "facebook", "toolbar=no, width=550, height=550"); return false;' href='http://delicious.com/save?v=5&noui&jump=close&url=<?php echo $shareUrl; ?>&title=<?php echo $shareTitle; ?>' class='delicious'>
<?php
if($deliciousStats[0]->total_posts == 0) {
echo 'Save';
} elseif($deliciousStats[0]->total_posts == 1) {
echo 'One save';
} else {
echo $deliciousStats[0]->total_posts.' saves';
}
?>
</a>
I also got the API Url which calls the tweeted numbers and URL.
http://urls.api.twitter.com/1/urls/count.json?url=SOMESITEURLHERE&callback=twttr.receiveCount
Basically it calls the JSON encoded file, and then gives you the option to share the link in <A></A> tags but instead of showing some text such as Share, it will show the count instead. I'm basically creating some CSS share buttons.

Just use Twitter's own tweet button.
It does that for you and you can style it with .twitter-share-button
(I would post this as a reply but I don't have the privilege.)

Probably you have figured out a solution yourself by now. I just had the same problem and solved it this way:
$handle = fopen('http://urls.api.twitter.com/1/urls/count.json?url=nu.nl', 'rb');
$twitCount = json_decode(stream_get_contents($handle));
fclose($handle);
print_r($twitCount->count);

function get_tweets($url) {
$json_string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $url);
$json = json_decode($json_string, true);
return intval( $json['count'] );
}
function total($url){
return get_tweets($url); }
Then, use this to get the twitte share count in required place.
<?php echo total("http://website.com/"); ?>

Related

how to get a third variable using a doubled join variable in php

PHP CODE:
$video1="welcome.mp4";
$video2="movie.mp4";
$video3="ends.mp4";
$num_id="1";
$get = '$' . "video" . $num_id;
$file = $get;
echo "<a href=Player.php?file=$file'>Play</a>";
html code results:
Play
HTML - expectancy:
Play
I would do something like this.
$videos = ['welcome.mp4','movie.mp4','ends.mp4'];
foreach($videos as $video){
echo "<a href=Player.php?file=$video'>Play</a><br>";
}
I would do this (If I wanted to be Kool, or confuse a junior developer .. lol )
$video1="welcome.mp4";
$video2="movie.mp4";
$video3="ends.mp4";
$num_id="1";
$get = ${"video".$num_id};
$file = $get;
echo "<a href=Player.php?file=$file'>Play</a>";
Output
<a href=Player.php?file=welcome.mp4'>Play</a>
But I am not sure you are ready to have access to that much power...
Sandbox

How to determine whether my content has a instagram embed?

The posts in my site has video(single) from anyone of the following embeds.
Youtube
Facebook
Instagram
My question is while fetching them on front end I want to findo out whether my content has an embed, if so which of the following is embedded. (iframe presence checking is one (dirty)way still it own work for instagram)
PHPCODE:
$video_start = strpos($singlePost->post_content, "<iframe");//Get to the start of the iframe(video)
$video_stop = strpos($singlePost->post_content, "</iframe>");//Get to the end of the iframe(video)
$iframe_content = substr($singlePost->post_content, $video_start, $video_stop);
$xpath = new DOMXPath(#DOMDocument::loadHTML($iframe_content));
$iframe_src = $xpath->evaluate("string(//iframe/#src)");
$parsed_url = parse_url($iframe_src);
$host = $parsed_url['host'];
if(strpos($host, "youtube") !== false) { // If it is a youtube video append this
$iframe_src = $iframe_src."?rel=0";// This option has to be appended of youtube URL's
$related_social_icon = "youtube";
$related_social_media = "youtube";
}
<iframe class="<?php echo $iframe_class; ?>" src="<?php echo $iframe_src; ?>" style="background-size: cover;" allowfullscreen></iframe>
Above code works fine for youtube, but does not work for instagram coz when inserting instagram comes as blockquote tags,but if you echo them it will be straight away become iframe tags due to the script in it.
I would go for something like this:
add_filter('the_content', function($content) {
$identifier = '<embed';
if (strpos($content, $identifier) !== false) {
// identifier found
$content = '<h1>This page includes an embed</h1>'.$content;
}
return $content;
});
I'm not sure how your embeds look like, you are talking about iframes to. So you need to find some identifiers that you can check.
Your post probably got downvoted because it could have some more information?

Instagram URL --> JSON

How can I go about convering a public instagram URL into JSON using PHP? Ex: https://www.instagram.com/explore/tags/brindle/
I can't use the API as I need public hashtag content and my use case won't qualify for their app review process :-(.
Here is what I have so far but it does not pull all images. Also, I'd like to be able to load the "load more" images as well. Any help would be much appreciated!
$instagram_source = file_get_contents("https://www.instagram.com/explore/tags/brindle/");
$instagram_data = explode("window._sharedData = ", $instagram_source);
$instagram_json = explode(';</script>', $instagram_data[1]);
$instagram_array = json_decode($instagram_json[0], TRUE);
$instagram_media = $instagram_array['entry_data']['TagPage'][0]['tag']['media']['nodes'];
if(!empty($instagram_media)) {
echo '<ul>';
foreach($instagram_media as $im) {
echo '<li>';
echo '<a href="https://www.instagram.com/p/'.$im['code'].'/" target="_blank">';
echo '<img src="'.$im["display_src"].'" alt="" width="'.$im["dimensions"]["width"].'" height="'.$im["dimensions"]["height"].'" />';
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
Take a look at this solution here: https://github.com/Bolandish/Instagram-Grabber
Thats the best one i know until now.

display file name in while loop without url

I want to display only the file name without displaying the url.
I want to replace the file name instead of 'document '.
while($fet=mysql_fetch_assoc($sql1))
{
$i=$i+1;
$next=$fet['f_name'];
echo '<h4><a class="astext" href="'.$next.'" title="'.$next.'" target="_blank" download>Document'.$i.'</a></h4>';
}
if I replace the word 'document' to $next it shows full url as http://www.sample/txt/1/sample.doc I need to display sample.doc.
echo '<h4><a class="astext" href="'.$next.'" title="'.$next.'" target="_blank" download>"'.$next.'"</a></h4>';
There are two options.
Assume you have $next=http://www.sample/txt/1/sample.doc
Option 1:
This works if you think http://www.sample/txt/1/ is same to all folders.
ltrim($next,"http://www.sample/txt/1/");
Option 2:
use basename($next)
This will extract the sample.doc
<?php
$link = "http://www.sample/txt/1/sample.doc";
$linkArray = explode('/', $link);
echo $linkArray[count($linkArray)-1];
?>
Magesh Kumaar also provide good one
<?php
$link = "http://www.sample/txt/1/sample.doc";
echo basename($link);
?>
I included $next1 = basename($next);
while($fet=mysql_fetch_assoc($sql1))
{
$i=$i+1;
$next=$fet['f_name'];
$next1 = basename($next);
echo '<h4><a class="astext" href="'.$next.'" title="'.$next.'" target="_blank" download>'.$next1.'</a></h4>';
}
Its working now.
<?php
$url = "http://www.sample/txt/1/sample.doc";
echo strstr($url,"sample.");
?>
For More Details

Instagram Fetch Amount of Images API

A friend of mine wrote this script, displaying the 20 most recent instagram images, and I was wondering, how can I change the amount of images it grabs to maybe, 6?
<?PHP
$token = 'token';
$username = 'username';
$userInfo = json_decode(file_get_contents('https://api.instagram.com/v1/users/search?q='.$username.'&access_token='.$token));
if($userInfo->meta->code==200){
$photoData = json_decode(file_get_contents('https://api.instagram.com/v1/users/'.$userInfo->data[0]->id.'/media/recent/?access_token='.$token));
if($photoData->meta->code==200){ ?>
<?PHP foreach($photoData->data as $img){
echo '<img src="'.$img->images->thumbnail->url.'">';
} ?>
<?PHP } // If
} // If
?>
Now, the script is functional now because I've been working on it all day, but I'm not sure how to change how many it sends out.
Also, would any of you know how to style this? I already have the CSS done for it, but whenever I try it, it doesn't work correctly.
And, would you know how to get the description of the photo using the API?
Thank you in advance :-)
You need to use Instagram's count= url parameter when requesting data from their endpoints.
For example: https://api.instagram.com/v1/users/search?count=6
Or in your code:
<?PHP
$token = 'token';
$username = 'username';
$userInfo = json_decode(file_get_contents('https://api.instagram.com/v1/users/search?count=6&q='.$username.'&access_token='.$token));
if($userInfo->meta->code==200){
$photoData = json_decode(file_get_contents('https://api.instagram.com/v1/users/'.$userInfo->data[0]->id.'/media/recent/?count=6&access_token='.$token));
if($photoData->meta->code==200){ ?>
<?PHP foreach($photoData->data as $img){
echo '<img src="'.$img->images->thumbnail->url.'">';
} ?>
<?PHP } // If
} // If
?>
Pseudo example for styling. You'll need to figure out the css styles for that, but shouldn't be to difficult.
<div class='myBorder'>
<img url=$img->link />
<div class='myCaption'>$img->caption->text</div>
</div>
To get the description
if (isset($img->caption)) {
if (get_magic_quotes_gpc()) {
$title = stripslashes($img->caption->text);
} else {
$title = $img->caption->text;
}
}

Categories