Jump to content

Reading and Displaying Data from XML File


Techairlines

Recommended Posts

Hello again. I'm trying to load data from a Facebook Share Data XML file and then displaying them in a WordPress plugin. Here's my code so far:

 

// Fetch Share Count Data

function fb_button () {
$fbshareUrl = urlencode(get_permalink($post->ID));
$fbshareTitle = urlencode($post->post_title);
$fbLinkStats = simplexml_load_file('http://api.facebook.com/restserver.php?method=links.getStats&urls='.$shareUrl);

$fb_params =
        '?u=' . $fbshareUrl .
        '&t=' . $fbshareTitle .
        ''
        ;

// Vertical Facebook Button

       $fb_share = '
<div class="fsbsharer" id="fb">
<a href="http://www.facebook.com/sharer.php' . $fb_params . '"  class="facebookShare">

<span class="fb_share_size_Small fb_share_count_wrapper">
<span></span>

<span class="fb_share_count_nub_top "></span> 
<span class="fb_share_count  fb_share_count_top">


<div class="fb_share_count_inner">
'. $fbLinkStats->link_stat->total_count .'
</div>

</div>
<span style="cursor:pointer;" class="FBConnectButton FBConnectButton_Small"><span class="FBConnectButton_Text">Share</span></span>
</a>
</div>
';


       echo $fb_share;
    }

 

This code works for everything except the title and the share count. I can fix the title myself but the share count isn't working.

 

When I use this in plain HTML:

<?php echo $fbLinkStats->link_stat->total_count.''; ?>

it works fine, but when I combine it all into another function to echo, it no longer works properly.

 

Thanks.

Link to comment
Share on other sites

Hello. Thanks for the reply and sorry about the lack of information.

 

Right now on my own site, I'm not using the button as a plugin and here's the code for it:

<div class="sharer" id="fbshare">
		<?php 
$shareUrl = urlencode(get_permalink($post->ID));
$shareTitle = urlencode($post->post_title);
$fbLinkStats = simplexml_load_file('http://api.facebook.com/restserver.php?method=links.getStats&urls='.$shareUrl);
?>

<a onclick='window.open("http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&t=<?php echo $shareTitle; ?>", "facebook", "toolbar=no, width=550, height=550"); return false;' href='http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&t=<?php echo $shareTitle; ?>'  class='facebookShare'>

<span class="fb_share_size_Small fb_share_count_wrapper">
<span></span>

<span class="fb_share_count_nub_top "></span> 
<span class="fb_share_count  fb_share_count_top">

<div class="fb_share_count_inner">
<?php echo $fbLinkStats->link_stat->total_count.''; ?>
</div>
</div>
<span style="cursor:pointer;" class="FBConnectButton FBConnectButton_Small"><span class="FBConnectButton_Text">Share</span></span>
</a>

</div>

 

The line about echo $fbLinkStats->link_stat->total_count.''; works properly here and displays the share number. So if the Facebook XML file says the article was shared 5 times, it would display the number 5.

 

However, when I add it to another function in order to create a plugin, it doesn't display anything. Where a number should be is just a blank space.

function fb_button () {
$fbshareUrl = urlencode(get_permalink($post->ID));
$fbshareTitle = urlencode($post->post_title);
$fbLinkStats = simplexml_load_file('http://api.facebook.com/restserver.php?method=links.getStats&urls='.$shareUrl);

$fb_params =
        '?u=' . $fbshareUrl .
        '&t=' . $fbshareTitle .
        ''
        ;

// Vertical Facebook Button

       $fb_share = '
<div class="fsbsharer" id="fb">
<a href="http://www.facebook.com/sharer.php' . $fb_params . '"  class="facebookShare">

<span class="fb_share_size_Small fb_share_count_wrapper">
<span></span>

<span class="fb_share_count_nub_top "></span> 
<span class="fb_share_count  fb_share_count_top">


<div class="fb_share_count_inner">
'. $fbLinkStats->link_stat->total_count .'
</div>

</div>
<span style="cursor:pointer;" class="FBConnectButton FBConnectButton_Small"><span class="FBConnectButton_Text">Share</span></span>
</a>
</div>
';


       echo $fb_share;
    }

 

Thanks.

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.