1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

str_replace - Password protected XML file

Discussion in 'PHP' started by SEbasic, Jun 22, 2010.

  1. #1
    Hi there,

    OK so I'm comfortable with how str_replace works I think, but I need to put it into action on a live environment.

    I've got a live XML feed that I provide to our partners that sits behind a username/password.

    I've got a list of english terms that I need to translate for one partner in particular, along with their translated terms.

    The feed doesn't sit on a server that I have any control over at all.

    So I figure I'll need to use wget to grab the file, then str_replace to search for the strings that need replacing before I republish on our side and give it to our user.

    It's a live feed, so I need to be checking for changes every 30 seconds or so.Ideally I'm looking for something relatively off-the-shelf that I can use for this - I understand I might not be able to do everything I'm looking for in one go here, but if anyone has any ideas of where I can start with this it'd be massively appreciated - Id really rather not start off from scratch here.

    Has anyone got any experience with this kind of thing that might be able to offer some advice?

    Thanks in advance.
     
    SEbasic, Jun 22, 2010 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    From what I can tell that is the way you would need to do it. Every 30 seconds seems a bit extreme to me, I'd go for a minute at the minimum
     
    JAY6390, Jun 23, 2010 IP
  3. SEbasic

    SEbasic Peon

    Messages:
    6,317
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for that - please I've at least got the principal right, heh.

    Do you know of anything out there that's already been written & might make my life a little easier? While I think I'll be able to do it, it'll take me an age without the starting blocks.
     
    SEbasic, Jun 23, 2010 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Not really that I can think of. I don't see why this is so difficult if it's just replacing words with other words
     
    JAY6390, Jun 23, 2010 IP
  5. SEbasic

    SEbasic Peon

    Messages:
    6,317
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Well it's not hard to do each of those tasks individually, it's linking them together that I'm having the problem with.

    So I started off with

    <?php
    system('wget --user=USER --password=PASS http://example.com/feed.xml -O livefeed.xml');
    ?>

    So that wgets the feed & saves it as a file called livefeed.xml

    Awesome.

    But I figure I need to save the contents of the feed as a variable, so I've done this:


    <?php
    $feeddata = system('wget --user=USER --password=PASS http://example.com/feed.xml');
    ?>

    Now there are a few things I need to replace as I'd mentioned, but just to get going I'm going to look for just one of the strings that need replacing:

    <?php
    $feeddata = system('wget --user=USER --password=PASS http://example.com/feed.xml');
    $replaceone = str_replace("STRING", "REPLACESTRING", $feeddata);

    echo $replaceone;
    ?>

    *Obviously* this doesn't work...

    Can you tell I'm not all that good at this?

    Really I need to know how to link that variable I created with the str_replace function, then have that save in the file that would have been created by wget.

    I'm also conscious that I should be doing this using an array rather than multiple instances of str_replace as that's just bound to get messy.

    Thanks for the help so far.
     
    SEbasic, Jun 23, 2010 IP
  6. SEbasic

    SEbasic Peon

    Messages:
    6,317
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #6
    OK this works:


    <?php
    system('wget --user=XXX --password=XXX http://example.com.xml -O livefeed.xml');

    $feeddata = file_get_contents("livefeed.xml");

    $fp = fopen("livefeed.xml","w");

    $feeddata = str_replace("STRING1", "REPLACESTRING", $feeddata);
    $feeddata = str_replace("STRING1", "REPLACESTRING", $feeddata);


    fwrite($fp,$feeddata);
    fclose($fp);
    ?>


    Ta!
     
    SEbasic, Jun 23, 2010 IP
  7. archgames.net

    archgames.net Well-Known Member

    Messages:
    532
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    120
    #7
    Use curl to get the file (XML) content ( and you can authenticate using CURL aswell)

    then replace the data inside and fwrite with new content
     
    archgames.net, Jun 23, 2010 IP
  8. SEbasic

    SEbasic Peon

    Messages:
    6,317
    Likes Received:
    318
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ok can do - why use CURL though out of interest?
     
    SEbasic, Jun 23, 2010 IP