2012-02-14 15 views
9

Tengo problemas para hacer que la biblioteca cURL de PHP reconozca el alias que creé en mi archivo /etc/hosts.PHP cURL no ve el archivo/etc/hosts

Esto es lo que tengo en mi archivo /etc/hosts en este momento:

192.168.0.20 www.example.dev 

En el otro lado (192.168.0.20) Apache está configurado para ejecutar la máquina virtual en el dominio example.dev. El alias funciona si lo pruebo en mi navegador pero con PHP cURL simplemente no funciona.

El archivo es hosts en ambas máquinas (192.168.0.10 < cli = PHP, Apache 192.168.0.20 < =).

Para completar, este es el código PHP que estoy usando.

 $this->url = 'http://www.example.dev/'; 
     $this->ch = curl_init(); 
     $header = array 
     (
      "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
      "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3", 
      "Accept-Encoding: gzip,deflate,sdch", 
      "Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4", 
      "Cache-Control: max-age=0", 
      "Connection: keep-alive", 
     ); 

     $sUserAgent = $this->tor ? UserAgents::getRandom() : UserAgents::CHROME16_LINUX; 

     curl_setopt($this->ch, CURLOPT_HTTPHEADER, $header); 
     curl_setopt($this->ch, CURLOPT_USERAGENT, $sUserAgent); 
     curl_setopt($this->ch, CURLOPT_URL, $this->url); 
     curl_setopt($this->ch, CURLOPT_HEADER, false); 
     curl_setopt($this->ch, CURLINFO_HEADER_OUT, true); 
     curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true); 
     curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, (bool) $waitResponse); 
     curl_setopt($this->ch, CURLOPT_VERBOSE, (bool) $this->verbose); 
     curl_setopt($this->ch, CURLOPT_PORT, $this->port); 
     curl_setopt($this->ch, CURLOPT_AUTOREFERER, true); 
     curl_setopt($this->ch, CURLOPT_MAXREDIRS, 10); 
     curl_setopt($this->ch, CURLOPT_ENCODING, ''); 
     curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 30); 
     curl_setopt($this->ch, CURLOPT_TIMEOUT, 60); 
     curl_setopt($this->ch, CURLOPT_DNS_CACHE_TIMEOUT, 120); 
     curl_setopt($this->ch, CURLOPT_COOKIESESSION, true); 
     curl_setopt($this->ch, CURLOPT_COOKIEFILE, 'cookie'); 

     foreach ($this->files as $k => $file) { 
      $this->data['_file_' . $k] = '@' . $file; 
     } 

     curl_setopt($this->ch, CURLOPT_POST, true); 
     curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->data); 

     $this->result = curl_exec($this->ch); 

Nota: Este problema parece que this one pero relacionados con PHP.

+0

¿Ha reiniciado el servidor? ¿Solo por asegurar? –

+2

es el archivo de hosts en la máquina correcta? ¿ejecutas PHP en esta máquina? –

+0

¿Se puede publicar el código exacto? No debería haber un problema – mlinuxgada

Respuesta

6

resolver utilizando esta URL "http://192.168.0.20/" en lugar de "http://www.example.dev"

También el "host" se necesita cabecera ...

$header = array 
(
    "Host: www.example.dev", // IMPORTANT 
    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
    "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3", 
    "Accept-Encoding: gzip,deflate,sdch", 
    "Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4", 
    "Cache-Control: max-age=0", 
    "Connection: keep-alive", 
); 

curl_setopt($this->ch, CURLOPT_URL, 'http://192.168.0.20/'); 
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $header); 
2

Probablemente ejecute PHP en ese servidor Apache, y allí no tiene la entrada del archivo hosts.

+0

El archivo hosts está en todas las máquinas (192.168.0.10 <= php cli, 192.168.0.20 <= apache) –

+0

@karoly gracias! ¡esto me ilumina! ¡Estaba usando Docker para las pruebas y el contenedor no tenía la entrada en el archivo de hosts! – mloureiro