Problem in fopen()

Discussion in 'PHP' started by zerandib, May 31, 2009.

  1. #1
    I'm tryng to read the source using fopen()
    but, it is not working with this code! (Not work in webhosting account or localhost)
    any idea of , why such thing occurs!!! :confused:

    <?php
    	
    $fh = fopen("http://www.pornhub.com", "r") or die('Failed to open ***** !');
    
    $fullstring;
    while(!feof($fh))
    {
        $output = htmlspecialchars(fgets($fh, 1024));
    
    	$fullstring = $fullstring."".$output; 
    	
    }
    
    echo "**hi**";
    echo "<br/>";
    echo $fullstring;
    ?>
    Code (markup):

    (Note: if the site url is a new one for example: redtube.com - then it works fine)
    How to resolve this?
    Thanks
     
    zerandib, May 31, 2009 IP
  2. givemeknol

    givemeknol Active Member

    Messages:
    22
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    88
    #2
    Do you try to use "file_get_contents" function ?
     
    givemeknol, May 31, 2009 IP
  3. zerandib

    zerandib Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This wont do !
    actually i need to read the whole source using fopen.....

    any idea:rolleyes:
     
    zerandib, May 31, 2009 IP
  4. onpub

    onpub Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I use your's code , i can get source code www dot pornhub.com ..
     
    onpub, Jun 1, 2009 IP
  5. onpub

    onpub Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    and change is example: redtube.com , I can read it.
     
    onpub, Jun 1, 2009 IP
  6. onpub

    onpub Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Or $string = file_get_contents($url);
     
    onpub, Jun 1, 2009 IP
  7. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Try the CURL method (this function was designed to replace file_get_content but allowing users to simply rename their function to file_get_content_curl())

    
    function file_get_contents_curl($url) {
    	$ch = curl_init();
    	
    	curl_setopt($ch, CURLOPT_HEADER, 0);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
    	curl_setopt($ch, CURLOPT_URL, $url);
    	
    	$data = curl_exec($ch);
    	curl_close($ch);
    	
    	return $data;
    }
    
    PHP:
    Above is probably going to be your best bet since most webservers opting for security, is going to disable fopen being able to remotely open files, as well as disabling the file_get_contents function.
     
    kblessinggr, Jun 1, 2009 IP
  8. tguillea

    tguillea Active Member

    Messages:
    229
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #8
    Make sure your php.ini settings allow URLs in fopen()?
     
    tguillea, Jun 1, 2009 IP
  9. zerandib

    zerandib Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    yh kbless.....
    this works. but it displays the whole website.
    Is there a way to get the source-code from that!...

    In the return $data; section i want to get the source-code and display (echo) that on the screen other than the site.

    function file_get_contents_curl($url) {
        $ch = curl_init();
       
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
        curl_setopt($ch, CURLOPT_URL, $url);
       
        $data = curl_exec($ch);
        curl_close($ch);
       
        return $data;
    }
    Code (markup):
    thankx
     
    zerandib, Jun 3, 2009 IP