如何使用php curl为特定的ip设置主机名

时间:2012-03-29 20:03:09

标签: php curl

您好我有一台服务器,其上设置了多个虚拟主机。

我想使用php向此服务器的ip发出curl请求。 此外,我想将此请求发送到服务器的ip上的特定主机名。

有办法吗?

更详细一点: 我想使用内部IP在我的服务器之间使用内部IP发出curl请求。问题是我在这台服务器上托管了几个站点。所以,当我向服务器的内部IP发出一个curl请求时...(curl_init(xxx.xxx.xxx.xxx)),我希望能够告诉apache去一个指向的特定文件夹虚拟主机。我希望这个问题能够更加清晰...... - Vishesh Joshi 3分钟前编辑

3 个答案:

答案 0 :(得分:50)

您可以在curl请求中设置主机头:

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

答案 1 :(得分:21)

对于HTTPS站点,使用每个PHP版本since PHP 5.5中都存在的CURLOPT_RESOLVE

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

示例输出:

* 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...

答案 2 :(得分:14)

基于Leigh Simpson, 它工作,但我需要查询字符串附加它。 这就是我的工作:

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