Search code examples
phpcurlfile-get-contents

How to get contents of IP address in PHP


Im trying to use PHP file_get_contents for an IP. Example:

echo file_get_contents("1.22.22:2334/pay");

However PHP And Curl does not return the contents of the IP address, How would i go about doing this?

Thank you very much.


Solution

  • You should use curl as file_get_contents requires allow_url_fopen to be on in the php.ini config file (which is not always the case).

    With curl and IP only it should be like:

        // create curl resource 
        $ch = curl_init(); 
    
        // headers for ip only
        $headers = array("Host: www.myfaketopsite.com");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_URL, "https://1.22.22:2334/pay");
    
        //return the transfer as a string 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    
        // $output contains the output string 
        $output = curl_exec($ch); 
    
        // close curl resource to free up system resources 
        curl_close($ch);      
    

    Answer partly from: PHP cURL connect using IP address instead domain name

    More information: https://www.php.net/manual/de/book.curl.php