Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6871047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:45:28+00:00 2026-05-27T03:45:28+00:00

I’m completely at a loss on this one… I’ve written a function which first

  • 0

I’m completely at a loss on this one… I’ve written a function which first checks to see if a XML file of cached tweets exists in a cache directory, then retrieves tweets either from the file or via cURL. The problem is, the data that’s being stored into the XML file is prepending and appending a few characters which is breaking my SimpleXMLElement.

Here’s the function in its entirety:

<?php
function wpg_get_tweets($user, $number) {
  $cache = false;
  $cPath = get_theme_root().'/'.strtolower(get_current_theme()).'/cache/tweets.xml';
  if(file_exists($cPath)) {
    $modtime = filemtime($cPath);
    $timeago = time() - 600;
    if($modtime < $timeago) {
      $cache = false;
    }
    else {
      $cache = true;
    }
  }

  if($cache === false) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://twitter.com/statuses/user_timeline/'.$user.'.xml?count='.$number);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $content = curl_exec($ch);
    curl_close($ch);
    if($content === false) {
      if(filesize($cPath) != 0) {
        $content = file_get_contents($cPath);
      }
      else {
        $content = 
          '<?xml version="1.0" encoding="UTF-8"?>
           <statuses type="array">
            <status>
              <created_at>'.date('D M d h:i:s O Y').'</created_at>
              <id>138138002546364417</id>
              <text>Twitter API Issue - Users may currently be experiencing some site issues; our engineers are working on it.</text>
            </status>
          </statuses>';
      }
    }
    $fp = fopen($cPath, 'w');
    if(flock($fp, LOCK_EX)) {
      fwrite($fp, serialize($content));
      flock($fp, LOCK_UN);
    }
    fclose($fp);
  }
  else {
    $content = file_get_contents($cPath);
  }
  $data = strstr($content, '<?');
  $xml = new SimpleXMLElement($data);
  return $xml;
}
?>

Here’s an example of the data that’s stored in the XML file if it doesn’t exist or is out of date:

s:8200:"<?xml version="1.0" encoding="UTF-8"?>
<statuses type="array">
  <status>
    <created_at>Sun Nov 20 06:14:00 +0000 2011</created_at>
    <id>138138002546364417</id>
    <text>This is just some random text</text>
    <source>web</source>
    <truncated>false</truncated>
    <favorited>false</favorited>
    <in_reply_to_status_id></in_reply_to_status_id>
    <in_reply_to_user_id></in_reply_to_user_id>
    <in_reply_to_screen_name></in_reply_to_screen_name>
    <retweet_count>0</retweet_count>
    <retweeted>false</retweeted>
    <user>
      <id>1234</id>
      <name>Random Guy</name>
      <screen_name>UserName</screen_name>
      <location>Interwebs, Milky Way</location>
      <description>I like peanuts</description>

<profile_image_url>http://a2.twimg.com/profile_images/1234/avatar_normal.jpg</profile_image_url>
      <profile_image_url_https>https://si0.twimg.com/profile_images/1234/avatar_normal.jpg</profile_image_url_https>
      <url></url>
      <protected>false</protected>
      <followers_count>1233</followers_count>
      <profile_background_color>1e1810</profile_background_color>
      <profile_text_color>a83d22</profile_text_color>
      <profile_link_color>3e5e2f</profile_link_color>
      <profile_sidebar_fill_color>33291e</profile_sidebar_fill_color>
      <profile_sidebar_border_color>000000</profile_sidebar_border_color>
      <friends_count>874</friends_count>
      <created_at>Thu Feb 19 05:08:38 +0000 2009</created_at>
      <favourites_count>2</favourites_count>
      <utc_offset>-21600</utc_offset>
      <time_zone>Central Time (US &amp; Canada)</time_zone>
      <profile_background_image_url>http://a0.twimg.com/profile_background_images/1234/tbg.png</profile_background_image_url>
      <profile_background_image_url_https>https://si0.twimg.com/profile_background_images/1234/tbg.png</profile_background_image_url_https>
      <profile_background_tile>false</profile_background_tile>
      <profile_use_background_image>true</profile_use_background_image>
      <notifications></notifications>
      <geo_enabled>true</geo_enabled>
      <verified>false</verified>
      <following></following>
      <statuses_count>2309</statuses_count>
      <lang>en</lang>
      <contributors_enabled>false</contributors_enabled>
      <follow_request_sent></follow_request_sent>
      <listed_count>31</listed_count>
      <show_all_inline_media>false</show_all_inline_media>
      <default_profile>false</default_profile>
      <default_profile_image>false</default_profile_image>
      <is_translator>false</is_translator>
    </user>
    <geo/>
    <coordinates/>
    <place/>
    <contributors/>
  </status>
</statuses>
";

The problem seems to be the s:8200:" at the very beginning of the file and/or the "; at the very end of the file… I’m unsure of how to either remove this before creating the SimpleXMLElement or preventing it from being stored in the first place?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:45:29+00:00Added an answer on May 27, 2026 at 3:45 am

    Here’s the culprit:

    fwrite($fp, serialize($content));
    

    The extra stuff you’re seeing is the output of PHP’s serialize; it marks the output as being a string of 8,200 characters.

    Since it looks like $content will always be a string, you probably just want to do:

    fwrite($fp, $content);
    

    or, if you really want to store serialized strings, you need to unserialize when you read it back in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.