0

私はこれに完全に戸惑っています...最初にキャッシュされたツイートのXMLファイルがキャッシュディレクトリに存在するかどうかを確認し、次にファイルまたはcURLを介してツイートを取得する関数を作成しました。問題は、XMLファイルに格納されているデータが、SimpleXMLElementを壊しているいくつかの文字を先頭に追加していることです。

関数全体は次のとおりです。

<?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;
}
?>

XMLファイルが存在しないか、古くなっている場合にXMLファイルに保存されるデータの例を次に示します。

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>
";

問題s:8200:"は、ファイルの最初および/またはファイルの";最後にあるようです...作成する前にこれを削除する方法、SimpleXMLElementまたは最初に保存されないようにする方法がわかりません。 ?

4

2 に答える 2

1

犯人は次のとおりです。

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

あなたが見ている余分なものは、PHPの出力ですserialize。出力を8,200文字の文字列としてマークします。

$content常に文字列のように見えるので、おそらく次のことを実行するだけです。

fwrite($fp, $content);

または、本当にシリアル化された文字列を保存したい場合は、unserializeそれを読み戻すときに保存する必要があります。

于 2011-11-22T03:56:53.833 に答える
1

データをシリアル化していますが、プレーンテキストとして読み取っています。読み取りと書き込みの両方でそれぞれシリアル化と逆シリアル化するか、文字列として書き込み/読み取りを行います。

シリアル化/読み込みを行う場合は、シリアル化メタデータを削除します。

于 2011-11-22T03:57:16.533 に答える