Cómo establecer el nombre de host usando php curl para una ip específica


Hola tengo un servidor que tiene varios hosts virtuales configurados en él.

Quería hacer una petición curl a la ip de este servidor usando php. También quería hacer esta solicitud a un nombre de host específico en la ip del servidor.

¿Hay alguna manera de hacerlo?

Un poco más de elaboración : Quiero hacer peticiones curl entre mis servidores usando LAN interna, usando su IP interna. El problema es que tengo varios sitios alojados en este servidor. Así que cuando hago una solicitud de rizo a la IP interna del servidor.. algo así como(curl_init (xxx.xxx.xxx.xxx)), quiero poder decirle a apache que vaya a una carpeta en particular apuntada por un host virtual. Espero que eso haya dejado la pregunta un poco más clara.. - Vishesh Joshi hace 3 minutos edit

 31
Author: Vishesh Joshi, 2012-03-30

3 answers

Puede establecer el encabezado host en la solicitud 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);
 43
Author: simpleigh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-03-29 20:21:44

Para los sitios HTTPS use CURLOPT_RESOLVE que existe en todas las versiones de PHP desde 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);

Salida de muestra:

* 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...
 15
Author: sanmai,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-10 01:00:07

Base en Leigh Simpson, Funciona, pero necesito adjuntar cadena de consulta con él. Eso es lo que trabajo alrededor:

<?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);
?>
 12
Author: temple,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-18 19:42:32