PHP ini file_get_contents external url

后端 未结 7 1779
攒了一身酷
攒了一身酷 2020-11-22 15:28

I use following PHP function:

file_get_contents(\'http://example.com\');

Whenever I do this on a certain server, the result is empty. When I do

相关标签:
7条回答
  • 2020-11-22 15:46

    Add:

    allow_url_fopen=1
    

    in your php.ini file. If you are using shared hosting, create one first.

    0 讨论(0)
  • 2020-11-22 15:48

    This will also give external links an absolute path without having to use php.ini

    <?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://www.your_external_website.com");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    $result = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#",'$1http://www.your_external_website.com/$2$3', $result);
    echo $result
    ?>
    
    0 讨论(0)
  • 2020-11-22 15:52

    The is related to the ini configuration setting allow_url_fopen.

    You should be aware that enable that option may make some bugs in your code exploitable.

    For instance, this failure to validate input may turn into a full-fledged remote code execution vulnerability:

    copy($_GET["file"], "."); 
    
    0 讨论(0)
  • 2020-11-22 15:55

    Complementing Aillyn's answer, you could use a function like the one below to mimic the behavior of file_get_contents:

    function get_content($URL){
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_URL, $URL);
          $data = curl_exec($ch);
          curl_close($ch);
          return $data;
    }
    
    echo get_content('http://example.com');
    
    0 讨论(0)
  • 2020-11-22 15:56

    The setting you are looking for is allow_url_fopen.

    You have two ways of getting around it without changing php.ini, one of them is to use fsockopen(), and the other is to use cURL.

    I recommend using cURL over file_get_contents() anyways, since it was built for this.

    0 讨论(0)
  • 2020-11-22 16:01
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://www.your_external_website.com");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    

    is best for http url, But how to open https url help me

    0 讨论(0)
提交回复
热议问题