Fetching content from a different url

Discussion in 'Programming' started by pavelbarchuk, May 10, 2009.

  1. #1
    I have a page setup on my website so i can fetch it and use it on multiple websites. This is the code that i tried using and it doesnt seem to work.

    <table>
    <tr>
    <td>
    <?=file_get_contents ('http://www.domain.com/index2.php');?>
    </td>
    </tr>
    </table>

    Is there a different way of fetching php content?
     
    pavelbarchuk, May 10, 2009 IP
  2. alfa_375

    alfa_375 Active Member

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #2
    To fetch the content of other page
    1. You should have the file name with the extension of .php (example.php)
    2. Use the following code

    To fetch the content of other page
    1. You should have the file name with the extension of .php (example.php)
    2. Use the following code
    
    <table>
    <tr>
    <td>
    <?php  include ('http://www.domain.com/index2.php'); ?>
    </td>
    </tr>
    </table>
    
    
    Code (markup):
     
    alfa_375, May 12, 2009 IP
  3. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    alfa_375 you can't include a php from a different domain, the code will have already been parsed by the other server, and the include() function is expecting valid PHP code, not html.

    Normally <?=file_get_contents ('http://www.domain.com/index2.php');?> would work.

    But in cases where your server doesn't support that function, or its not allowed, there's the curl method, for example this function that acts like file_get_contents().

    
    function file_get_contents_curl($url) {
    	$ch = curl_init();
     
    	curl_setopt($ch, CURLOPT_HEADER, 0);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_URL, $url);
     
    	$data = curl_exec($ch);
    	curl_close($ch);
     
    	return $data;
    }
    
    PHP:
    As far as your question goes " fetching php content", keep in mind that if you try to grab a php file from another webserver over http protocol, the php code will become executed on that server and what you grab will only be the resulting html output.
     
    kblessinggr, May 12, 2009 IP
  4. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    By the way... did you know you had a space in between file_get_contents and the first ( ... that would be a problem if you actually had a space there (as its a function call, not a if () etc).
     
    kblessinggr, May 12, 2009 IP
  5. jason146

    jason146 Peon

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    you should have to use the curl library to fetch the contents of other site and regular expressions to extract only some part of site....
     
    jason146, May 12, 2009 IP
  6. digitalhost

    digitalhost Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    CURL() is best option to fetch data from other site. You need to enable curl on your server.

    You can also display content of other site by using your own code which will parse page and will display only specific contents on your website.
     
    digitalhost, May 13, 2009 IP
  7. dias.ocean

    dias.ocean Peon

    Messages:
    71
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I agree, cURL is the best option.
     
    dias.ocean, May 13, 2009 IP
  8. ankit_frenz

    ankit_frenz Active Member

    Messages:
    1,111
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    63
    #8
    curl is best...still you can try fopen as well
     
    ankit_frenz, May 13, 2009 IP
  9. Fareast

    Fareast Banned

    Messages:
    64
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    i agree yes use curl is the better way to fetch website, but some times file get contents is more better than curl. i like to use file_get_contents .
     
    Fareast, May 15, 2009 IP
  10. jhbalaji

    jhbalaji Well-Known Member

    Messages:
    1,474
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    140
    #10
    Yes cURL is best but some hosts have disabled it due to security reasons mate...
    so careful.......
     
    jhbalaji, May 23, 2009 IP
  11. tailender1

    tailender1 Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    most of the time on shared hosting all the funtions like file_get_contents, fopen(url) are disabled due to high probability of abuse :(
     
    tailender1, May 30, 2009 IP