19

I have an website. Let's call it http://www.domain.com. Now, on this domain.com I want to display the file contents of http://www.adserversite.com/ads.php. How can I do that with cURL or another method? I don't want to use iframe.

Thanks

Andrei RRR
  • 3,068
  • 16
  • 44
  • 75
  • Possible duplicate of the **Related Section**. Please do research before asking questions that have been asked already or are easily answerable by googling. – Gordon Oct 08 '12 at 13:18

2 Answers2

56

You can use file_get_contents as Petr says, but you need to activate allow_url_fopen in your php.ini and perhaps your hosting do not allow you to change this.

If you prefer to use CURL instead of file_get_contents, try this code:

<?php
$url = 'http://www.adserversite.com/ads.php';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
m4t1t0
  • 5,669
  • 3
  • 22
  • 30
  • 1
    Although true and completely accurate, it might be useful for full disclosure that using cURL has the requirement of having the cURL extension installed, where `allow_url_fopen` is on by default ;) – Berry Langerak Oct 08 '12 at 12:57
  • 2
    You are right, file_get_contents is preferible, but I encountered problems in the past with shared hostings. – m4t1t0 Oct 08 '12 at 13:51
  • 1
    With PHP 7 I am having trouble with both approaches allow_url_fopen is on an curl is installed. – Wolfgang Fahl Aug 30 '16 at 10:04
  • Is there any way to read the data line by line? I seem to get a big mess of text read from the file. – uplearned.com Jul 28 '19 at 04:47
12
echo file_get_contents('http://www.adserversite.com/ads.php');

Who needs curl for this simple task?

Petr
  • 3,214
  • 18
  • 21
  • 16
    Note: if `allow_url_fopen` is "On" in php.ini, which may not be true on some shared hostings, like @m4t1t0 mentioned already. I personally faced several times situation when curl was installed, but `allow_url_fopen` was disabled. – zergussino Jun 06 '13 at 11:54
  • 1
    Also note that file_get_contents is much slower then cURL! http://stackoverflow.com/questions/13004805/file-get-contents-or-curl-in-php/24954327#24954327 – CodeBrauer Feb 18 '15 at 08:26
  • 1
    Also note that curl does properly check for SSL certificates by default - PHP only does the right thing since version 5.6, and cannot support SubjectAlternativeName fields in earlier versions. – Sven Aug 03 '16 at 16:31