【问题标题】:How to get contents of IP address in PHP如何在PHP中获取IP地址的内容
【发布时间】:2019-08-25 05:24:55
【问题描述】:

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

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

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

非常感谢。

【问题讨论】:

  • IP 地址是公共的还是内部的?那个ip是否也设置为共享内容?什么时候返回?
  • 这至少需要一个协议前缀http://…,以便 IP 解析为实际服务。 IP 本身什么都不做
  • 你能澄清一下“获取IP地址的内容”是什么意思吗? IP 地址只是一个数字。我猜您想从具有该地址的机器上运行的服务请求一些数据 - 什么服务?通过哪个协议?
  • @Joni,我想他想对这个 url 进行 curl 调用。

标签: php curl file-get-contents


【解决方案1】:

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

只有 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

【讨论】:

    猜你喜欢
    • 2010-12-15
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    相关资源
    最近更新 更多