To download the contents of a web page - php code - need help

Discussion in 'PHP' started by ichkoguy, Jan 19, 2009.

  1. #1
    Hi all,

    I am a newbie in php. I want to know the code to download the source code of a webpage when its url is specified in the php program.
     
    ichkoguy, Jan 19, 2009 IP
  2. NinjaWork

    NinjaWork Guest

    Messages:
    132
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    the function "fopen" will do that
     
    NinjaWork, Jan 19, 2009 IP
  3. bigrollerdave

    bigrollerdave Well-Known Member

    Messages:
    2,112
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    140
  4. ichkoguy

    ichkoguy Active Member

    Messages:
    666
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #4

    So this tag works for a file to get as input. It takes a file. but am not sure whether this function is useful in downloading the source code when i give a url like "http://www.google.com". So will this work under this condition?
     
    ichkoguy, Jan 19, 2009 IP
  5. bigrollerdave

    bigrollerdave Well-Known Member

    Messages:
    2,112
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    140
    #5
    Okay let me try and understand you better.

    You want to put in a url and then be prompted to download the source code?
     
    bigrollerdave, Jan 19, 2009 IP
  6. ichkoguy

    ichkoguy Active Member

    Messages:
    666
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Yes dude. If i mention any url link, the program should download the source code of the corresponding web page.

    I have the coding in java. But not in php. For example, i will give the java coding here:
    import java.io.*;
    import java.util.*;
    import java.net.*;


    public class Downloadsourcecode
    {
    public static void main(String arg[]) throws Exception
    {
    int c;
    URL hp=new URL("http://www.yahoo.com");
    URLConnection hpc=hp.openConnection();
    InputStream input=hpc.getInputStream();
    System.out.println("Content");

    while((c=input.read())!=-1)
    System.out.print((char)c);

    input.close();
    }
    }


    Just try running this code. You will get the output as a source code of the specified web site address.
     
    ichkoguy, Jan 19, 2009 IP
  7. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #7
    $ch = curl_init();
    $options = array(CURLOPT_FOLLOWLOCATION=>0, 
    CURLOPT_USERAGENT=>$_SERVER['HTTP_USER_AGENT']); 
    curl_setopt ($ch, CURLOPT_URL, $login_url);
    curl_setopt_array ($ch, $options);
    $page = curl_exec ($ch);
    Code (markup):
    $page will contain the source code.

    Thanks.
     
    harrisunderwork, Jan 19, 2009 IP
  8. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    So are you saying this code can be used 2 view the PHP source code of any PHP site?
     
    Omzy, Jan 19, 2009 IP
  9. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #9
    No, only the HTML source.
     
    Danltn, Jan 19, 2009 IP
  10. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    PHEW! I almost fell of my chair after seeing this thread lol. :D
     
    Omzy, Jan 19, 2009 IP
  11. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #11
    Lolz, curl sees that output only that a browser see. So now confusion should be there whether is it PHP source code or HTML source code.

    OFCOURSE IT'S A HTML SOURCE CODE :p
     
    harrisunderwork, Jan 19, 2009 IP
  12. ichkoguy

    ichkoguy Active Member

    Messages:
    666
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #12
    So what if the page has got lots of other scripts like javascript or php tags? I do not want only the html tags but all the coding tags from the source.
     
    ichkoguy, Jan 19, 2009 IP
  13. paitken

    paitken Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    It depends on what you call contents. Some will just get you the html. You will need to do a little more work if you want all the images, scripts, etc.
     
    paitken, Jan 19, 2009 IP
  14. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #14
    Well, if you curl a page, you will get all things, everything that is embedded inside the the page. No exceptions.
     
    harrisunderwork, Jan 20, 2009 IP
  15. ichkoguy

    ichkoguy Active Member

    Messages:
    666
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #15
    ichkoguy, Jan 20, 2009 IP
  16. WPGLamour

    WPGLamour Active Member

    Messages:
    30
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    95
    #16
    PHP is a server side script, so there is no way for a visitor to see the php code. Absolutely NO WAY.
    You can just see what the php code outputs to the browser.
    fopen and cURL are the best way to do that.
    I am sure the point of this is to copy content from sites not source code.
     
    WPGLamour, Jan 20, 2009 IP
  17. ichkoguy

    ichkoguy Active Member

    Messages:
    666
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #17

    Exactly dude. And you know guys, i have successfully finished the first module of my project with your help. Now that i have finished in java fully. Since i had only one day time, i went for java. But surely will try to do the same with php very soon once when i finish my entire project.
     
    ichkoguy, Jan 21, 2009 IP