www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > PHP

    PHP Discussion and technical support for using and deploying PHP based websites.

    Reply
     
    Thread Tools Rate Thread Display Modes
      #1  
    Old 04-10-2009, 12:57 PM
    Marcus22 Marcus22 is offline
    Registered User
     
    Join Date: Apr 2008
    Posts: 10
    resolved [RESOLVED] Using PHP to get/store Twitter statuses in xml

    Hi everybody,

    First of all sorry if there is a clear guide to this, but I tried to find something with google but couldn't find it. Maybe it's because I don't know the right terms to describe it.

    Here is my question. I have a javascript that reads my Twitter statuses from an .xml file, but there is a limit to the amount of posts/statusses it can handle. In default the Twitter xml feed displays 20posts, I need 5.

    Now here is what I want to do and why this is posted in the PHP forum. I need a php script that reads the xml from http://twitter.com/statuses/user_timeline/user_id.xml, saves it/stores it (only the first 5 statuses) to an index.xml file and refeshes every 15 or 30 minutes.

    Is this possible? If I didn't gave eough information or you need to know more, please tell me.

    Best regards,

    Marcus
    Reply With Quote
      #2  
    Old 04-10-2009, 02:39 PM
    Marcus22 Marcus22 is offline
    Registered User
     
    Join Date: Apr 2008
    Posts: 10
    A new post since I can't seem to edit my other post:

    I found out how to limit the posts/statuses, you just have to add ?count=5 to the Twitter link. So with that out of the way, I just need a script ( I think) that gets the data from the Twitter xml sheets, saves it to twitter.xml on my server and does that every 15 or 30 minutes. If possible it could check if actually a new post was Tweeted?

    Any help would be appreciated.

    You might ask why not just use the Twitterlink, but that's because it would slow down my site.

    Best regards,

    Marcus
    Reply With Quote
      #3  
    Old 04-10-2009, 04:06 PM
    Mindzai's Avatar
    Mindzai Mindzai is offline
    Registered User
     
    Join Date: Nov 2008
    Posts: 2,462
    If you just want to get a copy of the xml file, i'd look at curl.
    Reply With Quote
      #4  
    Old 04-12-2009, 02:04 PM
    Marcus22 Marcus22 is offline
    Registered User
     
    Join Date: Apr 2008
    Posts: 10
    Thank you for your help, sorry for my late response.

    Does it require something like that?

    I've made this script(see below), which reads and then writes the xml file. Isn't there something like this only to execute on each quarter of an hour? An If Else function?

    I am pretty new to PHP, I am still learning so I don't know if something like that would be possible? If not I guess I'll have to look into curl and maybe PEAR, but I already checked my host if it'd supports PEAR Cache module and it doesn't so that would be a problem.

    If it could be done in this way I could just include this script in the index file and I would be done, while maybe not the prettiest way to do it, it would do the job.

    Yours sincerely,

    Marcus



    PHP Code:
    <?php
    //--- load in  the xml ---
    $theRAW = file_get_contents("http://twitter.com/statuses/user_timeline/twitter.xml?count=4");
    $theXML = simplexml_load_string($theRAW);

    //--- load in xml file to edit.  if doesnt exist, create ---
    $xmlFile = "sitewide/js/dataservices/twitter/index.xml";
    $xmlHandle = fopen($xmlFile, "w");

    //--- set xml as string to be written ---
    $xmlString = $theXML->asXML();
    echo(
    $xmlString);

    //--- write xml to file ---
    fwrite($xmlHandle, $xmlString);

    //--- close out xml file ---
    fclose($xmlHandle);
    ?>
    Reply With Quote
      #5  
    Old 04-12-2009, 03:22 PM
    Marcus22 Marcus22 is offline
    Registered User
     
    Join Date: Apr 2008
    Posts: 10
    I can't seem to edit my last post for some reason, but I've looked into curl and this is what I came up with. Now my question is how to add a 15 to 30 min timeout on reconnecting/redownloading the file?
    I can't seem to figure that out, is there a CURLOPT for that? I see something with the modification date in CURL_TIMECOND_IFMODSINCE, CURLOPT_TIMEVALUE and CURLOPT_TIMECONDITION.

    Yours sincerely,

    Marcus



    PHP Code:
    <?php
    $username
    = "";
    $password = "";
    $twitterHost = "http://$username:$password@twitter.com/statuses/user_timeline/twitter.xml?count=4";
    $curl;
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($curl, CURLOPT_URL, $twitterHost);
    $result = curl_exec($curl);
    curl_close($curl);
    header('Content-Type: application/xml; charset=ISO-8859-1');
    $fname = "demo.xml";
    $fhandle = fopen($fname,"w");
    fwrite($fhandle,$result);
    fclose($fhandle);
    ?>
    Reply With Quote
      #6  
    Old 04-12-2009, 03:59 PM
    Mindzai's Avatar
    Mindzai Mindzai is offline
    Registered User
     
    Join Date: Nov 2008
    Posts: 2,462
    If you have a working script it is just a case of running it automatically using cron.
    Reply With Quote
      #7  
    Old 04-12-2009, 06:14 PM
    Marcus22 Marcus22 is offline
    Registered User
     
    Join Date: Apr 2008
    Posts: 10
    resolved

    Thank you very much, I got it working. It's updating every ten minutes now, I am very glad with the results and I think Twitter will be too
    How can I change the status to resolved?
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:13 PM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.