Note that there are some explanatory texts on larger screens.

plurals
  1. POstoring tweets as xml file
    primarykey
    data
    text
    <p>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.</p> <p>Here's the function in its entirety:</p> <pre><code>&lt;?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 &lt; $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 = '&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;statuses type="array"&gt; &lt;status&gt; &lt;created_at&gt;'.date('D M d h:i:s O Y').'&lt;/created_at&gt; &lt;id&gt;138138002546364417&lt;/id&gt; &lt;text&gt;Twitter API Issue - Users may currently be experiencing some site issues; our engineers are working on it.&lt;/text&gt; &lt;/status&gt; &lt;/statuses&gt;'; } } $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, '&lt;?'); $xml = new SimpleXMLElement($data); return $xml; } ?&gt; </code></pre> <p>Here's an example of the data that's stored in the XML file if it doesn't exist or is out of date:</p> <pre><code>s:8200:"&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;statuses type="array"&gt; &lt;status&gt; &lt;created_at&gt;Sun Nov 20 06:14:00 +0000 2011&lt;/created_at&gt; &lt;id&gt;138138002546364417&lt;/id&gt; &lt;text&gt;This is just some random text&lt;/text&gt; &lt;source&gt;web&lt;/source&gt; &lt;truncated&gt;false&lt;/truncated&gt; &lt;favorited&gt;false&lt;/favorited&gt; &lt;in_reply_to_status_id&gt;&lt;/in_reply_to_status_id&gt; &lt;in_reply_to_user_id&gt;&lt;/in_reply_to_user_id&gt; &lt;in_reply_to_screen_name&gt;&lt;/in_reply_to_screen_name&gt; &lt;retweet_count&gt;0&lt;/retweet_count&gt; &lt;retweeted&gt;false&lt;/retweeted&gt; &lt;user&gt; &lt;id&gt;1234&lt;/id&gt; &lt;name&gt;Random Guy&lt;/name&gt; &lt;screen_name&gt;UserName&lt;/screen_name&gt; &lt;location&gt;Interwebs, Milky Way&lt;/location&gt; &lt;description&gt;I like peanuts&lt;/description&gt; &lt;profile_image_url&gt;http://a2.twimg.com/profile_images/1234/avatar_normal.jpg&lt;/profile_image_url&gt; &lt;profile_image_url_https&gt;https://si0.twimg.com/profile_images/1234/avatar_normal.jpg&lt;/profile_image_url_https&gt; &lt;url&gt;&lt;/url&gt; &lt;protected&gt;false&lt;/protected&gt; &lt;followers_count&gt;1233&lt;/followers_count&gt; &lt;profile_background_color&gt;1e1810&lt;/profile_background_color&gt; &lt;profile_text_color&gt;a83d22&lt;/profile_text_color&gt; &lt;profile_link_color&gt;3e5e2f&lt;/profile_link_color&gt; &lt;profile_sidebar_fill_color&gt;33291e&lt;/profile_sidebar_fill_color&gt; &lt;profile_sidebar_border_color&gt;000000&lt;/profile_sidebar_border_color&gt; &lt;friends_count&gt;874&lt;/friends_count&gt; &lt;created_at&gt;Thu Feb 19 05:08:38 +0000 2009&lt;/created_at&gt; &lt;favourites_count&gt;2&lt;/favourites_count&gt; &lt;utc_offset&gt;-21600&lt;/utc_offset&gt; &lt;time_zone&gt;Central Time (US &amp;amp; Canada)&lt;/time_zone&gt; &lt;profile_background_image_url&gt;http://a0.twimg.com/profile_background_images/1234/tbg.png&lt;/profile_background_image_url&gt; &lt;profile_background_image_url_https&gt;https://si0.twimg.com/profile_background_images/1234/tbg.png&lt;/profile_background_image_url_https&gt; &lt;profile_background_tile&gt;false&lt;/profile_background_tile&gt; &lt;profile_use_background_image&gt;true&lt;/profile_use_background_image&gt; &lt;notifications&gt;&lt;/notifications&gt; &lt;geo_enabled&gt;true&lt;/geo_enabled&gt; &lt;verified&gt;false&lt;/verified&gt; &lt;following&gt;&lt;/following&gt; &lt;statuses_count&gt;2309&lt;/statuses_count&gt; &lt;lang&gt;en&lt;/lang&gt; &lt;contributors_enabled&gt;false&lt;/contributors_enabled&gt; &lt;follow_request_sent&gt;&lt;/follow_request_sent&gt; &lt;listed_count&gt;31&lt;/listed_count&gt; &lt;show_all_inline_media&gt;false&lt;/show_all_inline_media&gt; &lt;default_profile&gt;false&lt;/default_profile&gt; &lt;default_profile_image&gt;false&lt;/default_profile_image&gt; &lt;is_translator&gt;false&lt;/is_translator&gt; &lt;/user&gt; &lt;geo/&gt; &lt;coordinates/&gt; &lt;place/&gt; &lt;contributors/&gt; &lt;/status&gt; &lt;/statuses&gt; "; </code></pre> <p>The problem seems to be the <code>s:8200:"</code> at the very beginning of the file and/or the <code>";</code> at the very end of the file... I'm unsure of how to either remove this before creating the <code>SimpleXMLElement</code> or preventing it from being stored in the first place?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload