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

Vis*_*shi 36 php curl

嗨,我有一台服务器,上面有几个虚拟主机.

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

有办法吗?

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

sim*_*igh 52

您可以在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);
Run Code Online (Sandbox Code Playgroud)


san*_*mai 23

对于自PHP 5.5以来CURLOPT_RESOLVE每个PHP版本中存在的HTTPS站点使用.

<?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);
Run Code Online (Sandbox Code Playgroud)

样本输出:

* 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...
Run Code Online (Sandbox Code Playgroud)


tem*_*ple 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);
?>
Run Code Online (Sandbox Code Playgroud)