How to set hostname using php curl for a specific ip

后端 未结 3 1449
野性不改
野性不改 2020-12-13 03:56

Hi I have a server which has several virtual hosts set up on it.

I wanted to make a curl request to this server\'s ip using php. Also I wanted to make this request t

相关标签:
3条回答
  • 2020-12-13 04:34

    Base on Leigh Simpson, It works, but I need query string attach with it. That's what I work around:

    <?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://xxx.xxx.xxx.xxx/index.php?page=api&action=getdifficulty");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: subdomain.hostname.com'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    echo curl_exec($ch);
    ?>
    
    0 讨论(0)
  • 2020-12-13 04:40

    You can set the host header in the curl request:

    <?php
    $ch = curl_init('XXX.XXX.XXX.XXX');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: subdomain.hostname.com'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    echo curl_exec($ch);
    
    0 讨论(0)
  • 2020-12-13 04:46

    For HTTPS sites use CURLOPT_RESOLVE which exists in every PHP version since PHP 5.5.

    <?php
    $ch = curl_init('https://www.example.com/');
    // note: array used here
    curl_setopt($ch, CURLOPT_RESOLVE, array(
        "www.example.com:443:172.16.1.1",
    ));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    $result = curl_exec($ch);
    

    Sample output:

    * Added www.example.com:443:172.16.1.1 to DNS cache
    * Hostname www.example.com was found in DNS cache
    *   Trying 172.16.1.1...
    
    0 讨论(0)
提交回复
热议问题