如何在PHP中获取IP地址的内容

时间:2019-04-03 15:49:09

标签: php curl file-get-contents

我正在尝试将PHP file_get_contents用于IP。示例:

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

但是PHP And Curl不返回IP地址的内容,我该怎么做?

非常感谢您。

1 个答案:

答案 0 :(得分:0)

您应该使用curl,因为file_get_contents要求allow_url_fopen处于php.ini配置文件中(并非总是如此)。

只有curl和IP,它应该像:

    // 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);      

部分答案来自:PHP cURL connect using IP address instead domain name

更多信息:https://www.php.net/manual/de/book.curl.php