Link converter - how to run the script for multiple links?

Discussion in 'PHP' started by SwiftKiwi, Mar 26, 2010.

  1. #1
    Right, so I've just created a script that will take a rapidshare link, convert it to an ip address and then add the un:pw ready to download in a download manager. This works fantastically for a single link, but I want it to work for multiple links. eg For each link, run the script and then list all the converted links at the end.

    Here's my current code:

    <?
    
    include 'config.php';
    
    if ($_POST['links']) {
    
    $url = $_POST['links'];
    $page = file_get_contents($url);
    preg_match('/<form id="ff" action="(.*?)" method="post">/s', $page, $pre_link);
    $link = trim(html_entity_decode($pre_link[1]));
    
    function extract_numbers($string)
    {
    preg_match('/([\d]+)/', $string, $match);
    
    return $match[0];
    }
    
    $done = extract_numbers($link);
    
    $x = gethostbyname('rs'.$done.'.rapidshare.com');
    
    $iplink = preg_replace('/rapidshare.com/', $x, $url);
    
    $final = str_replace("http://" , "http://$un:$pw@" , $iplink);
    
    echo $final; 
    
    }
    else {
    ?>
    <form method="post" action="">
    <textarea name="links" cols="80" rows="20"></textarea> <br/>
    <input type="submit"/>
    </form>
    <? } ?>
    PHP:
    TIA
     
    SwiftKiwi, Mar 26, 2010 IP
  2. pig2cat

    pig2cat Active Member

    Messages:
    299
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #2
    put each link on a new line and cut up the input from the textarea
    
    $lines = explode("
    ", $_POST['links']);
    foreach($lines as $key => $val) {
    //do some validation if they're really links
    //run the function
    }
    
    PHP:
     
    pig2cat, Mar 27, 2010 IP
  3. SwiftKiwi

    SwiftKiwi Guest

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks pig2cat!

    Only problem is now that the extract_numbers will only run once, I get this error: Fatal error: Cannot redeclare extract_numbers()
     
    SwiftKiwi, Mar 27, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    replace the function with this:

    if(!function_exists('extract_numbers')){
    function extract_numbers($string)
    {
    preg_match('/([\d]+)/', $string, $match);
    
    return $match[0];
    }
    }
    
    PHP:
     
    danx10, Mar 27, 2010 IP
  5. SwiftKiwi

    SwiftKiwi Guest

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Cheers, that got rid of the fatal error but it's still giving the same IP address for multiple links :/
     
    SwiftKiwi, Mar 27, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    Untested, but should work:

    <?php
    
    include 'config.php';
    
    if ($_POST['links']) {
    
    if(!function_exists('extract_numbers')){
    function extract_numbers($string)
    {
    preg_match('/([\d]+)/', $string, $match);
    
    return $match[0];
    }
    }
    
    $url = $_POST['links'];
    
    $lines = str_replace(array("\r\n", "\r"), "\n", $url);
    
    $links = explode("\n", $lines);
    
    foreach($links as $link){
    $page = file_get_contents($url);
    preg_match('/<form id="ff" action="(.*?)" method="post">/s', $page, $pre_link);
    $link = trim(html_entity_decode($pre_link[1]));
    
    $done = extract_numbers($link);
    
    $x = gethostbyname('rs'.$done.'.rapidshare.com');
    
    $iplink = preg_replace('/rapidshare.com/', $x, $url);
    
    $final = str_replace("http://" , "http://$un:$pw@" , $iplink);
    
    echo $final."<br>";
    
    }
    }
    else {
    ?>
    <form method="post" action="">
    <textarea name="links" cols="80" rows="20"></textarea> <br/>
    <input type="submit"/>
    </form>
    <?php
    } 
    ?>
    PHP:
    Just make sure all links have their own line.
     
    danx10, Mar 27, 2010 IP
  7. SwiftKiwi

    SwiftKiwi Guest

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Still giving the same IP for both links :(
     
    SwiftKiwi, Mar 27, 2010 IP
  8. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #8
    <?php
    
    include 'config.php';
    
    if ($_POST['links']) {
    
    if(!function_exists('extract_numbers')){
    function extract_numbers($string)
    {
    preg_match('/([\d]+)/', $string, $match);
    
    return $match[0];
    }
    }
    
    $url = $_POST['links'];
    
    $lines = str_replace(array("\r\n", "\r"), "\n", $url);
    
    $links = explode("\n", $lines);
    
    foreach($links as $url){
    $page = file_get_contents($url);
    preg_match('/<form id="ff" action="(.*?)" method="post">/s', $page, $pre_link);
    $link = trim(html_entity_decode($pre_link[1]));
    
    $done = extract_numbers($link);
    
    $x = gethostbyname('rs'.$done.'.rapidshare.com');
    
    $iplink = preg_replace('/rapidshare.com/', $x, $url);
    
    $final = str_replace("http://" , "http://$un:$pw@" , $iplink);
    
    echo $final."<br>";
    
    }
    }
    else {
    ?>
    <form method="post" action="">
    <textarea name="links" cols="80" rows="20"></textarea> <br/>
    <input type="submit"/>
    </form>
    <?php
    }
    ?>
    PHP:
    Try that, made alittle mistake in previous code.
     
    danx10, Mar 27, 2010 IP
  9. SwiftKiwi

    SwiftKiwi Guest

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Working perfectly, thanks so much!
     
    SwiftKiwi, Mar 27, 2010 IP