Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Twitter & Facebook text links showing errors... sometimes?? WordPress

  • SOLVED

I have got both twitter and facebook text only links up on the site (, but every now and then they break... not at the same time though (I am assuming it is something to do with when facebook /or twitter is down)

The errors I get are these:

Facebook:
Warning: simplexml_load_file(http://api.facebook.com/restserver.php?method=links.getStats&urls=http%3A%2F%2Fdropdeadgorgeousdaily.com%2F2011%2F06%2Fguest-edit-designer-sara-phillips-just-cant-live-without%2F) [function.simplexml-load-file]: failed to open stream: HTTP request failed! in /var/www/vhosts/dropdeadgorgeousdaily.com/httpdocs/wp-content/themes/thesis_18/custom/custom_functions.php on line 335

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://api.facebook.com/restserver.php?metho (could't copy any more)

Twitter:

Warning: file_get_contents(http://api.tweetmeme.com/url_info?url=http://dropdeadgorgeousdaily.com/2011/06/if-you-only-buy-three-things-this-month-june-2011/) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /var/www/vhosts/dropdeadgorgeousdaily.com/httpdocs/wp-content/themes/thesis_18/custom/custom_functions.php on line 390

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /var/www/vhosts/dropdeadgorgeousdaily.com/httpdocs/wp-content/themes/thesis_18/custom/custom_functions.php:391 Stack trace: #0 /var/www/vhosts/dropdeadgorgeousdaily.com/httpdocs/wp-content/themes/thesis_18/custom/custom_functions.php(391): SimpleXMLElement->__construct('') #1 /var/www/vhosts/dropdeadgorgeousdaily.com/httpdocs/wp-content/themes/thesis_18/custom/custom_functions.php(431): tweetCount('http://dropdead...') #2 [internal function]: face_book_likeHP(1) #3 /var/www/vhosts/dropdeadgorgeousdaily.com/httpdocs/wp-includes/plugin.php(395): call_user_func_array('face_book_likeH...', Array) #4 /var/www/vhosts/dropdeadgorgeousdaily.com/httpdocs/wp-content/themes/thesis_18/lib/html/hooks.php(107): do_action('thesis_hook_aft...', 1) #5 /var/www/vhosts/dropdeadgorgeousdaily.com/httpdocs/wp-content/themes/thesis_18/lib/functions/content.php(211): thesis_hook_after_post(1) #6 /var/www/vhosts/dropdeadgorgeousdaily.com/httpdocs/wp-co in /var/www/vhosts/dropdeadgorgeousdaily.com/httpdocs/wp-content/themes/thesis_18/custom/custom_functions.php on line 391
Fatal Error (show)


Is there something I can add to the code so that if twitter or facebook is down it just shows blank?

Here is the code I have used:

Facebook:
<?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; ?>&amp;t=<?php echo $shareTitle; ?>", "facebook", "toolbar=no, width=550, height=550"); return false;' href='http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&amp;t=<?php echo $shareTitle; ?>' class='facebookShare'>

<?php
if($fbLinkStats->link_stat->total_count == 0) {
echo 'Share it!';
} elseif($fbLinkStats->link_stat->total_count == 1) {
echo 'One share';
} else {
echo $fbLinkStats->link_stat->total_count.' shares';
}
?>


Twitter:



<?php

function tweetCount($url) {



$content = file_get_contents("http://api.tweetmeme.com/url_info?url=".$url);



$element = new SimpleXmlElement($content);



$retweets = $element->story->url_count;



if($retweets)



return $retweets;



else

return 0;



}

?>





<?php echo tweetCount(get_permalink()); ?>


From previous Q - http://wpquestions.com/question/show/id/2395



Answers (1)

2011-06-08

Julian Lannigan answers:

Hi Kate,

I suggest adding a @ before the external functions.

Facebook:
@simplexml_load_file

Twitter:
@file_get_contents


Julian Lannigan comments:

This will tell PHP to ignore any errors or warnings that that particular function may produce. Although, you are going to want to add some if statements to make sure that the function provides the correct response and if it doesn't correct accordingly.


Julian Lannigan comments:

Twitter function:
<?php
function tweetCount($url) {
$content = @file_get_contents("http://api.tweetmeme.com/url_info?url=".$url);
if (empty($content))
return 0;

$element = new SimpleXmlElement($content);
$retweets = $element->story->url_count;

if($retweets)
return $retweets;
else
return 0;
}
?>


Julian Lannigan comments:

Facebook:

<?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; ?>&amp;t=<?php echo $shareTitle; ?>", "facebook", "toolbar=no, width=550, height=550"); return false;' href='http://www.facebook.com/sharer.php?u=<?php echo $shareUrl; ?>&amp;t=<?php echo $shareTitle; ?>' class='facebookShare'>

<?php
if(empty($fbLinkStats) || $fbLinkStats->link_stat->total_count == 0) {
echo 'Share it!';
} elseif($fbLinkStats->link_stat->total_count == 1) {
echo 'One share';
} else {
echo $fbLinkStats->link_stat->total_count.' shares';
}
?>


kateM82 comments:

Seems to be working... thanks yet again :)