2012-02-14 20 views
9

Sto riscontrando problemi nel rendere la libreria PHP cURL riconoscere l'alias che ho creato nel mio file /etc/hosts.PHP cURL non vede il file/etc/hosts

Questo è quello che ho nel mio file /etc/hosts in questo momento:

192.168.0.20 www.example.dev 

Dall'altro lato (192.168.0.20) Apache è configurato per eseguire l'host virtuale sul dominio example.dev. L'alias funziona se lo provo sul mio browser ma con PHP cURL non funziona.

Il file hosts è su entrambe le macchine (192.168.0.10 < = PHP CLI, 192.168.0.20 < = Apache).

Per motivi di completezza questo è il codice PHP che sto 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: Questo problema sembra this one ma PHP legati.

+0

Hai riavviato il server? Giusto per essere sicuri? –

+2

è il file hosts sulla macchina giusta? esegui PHP su questa macchina? –

+0

Puoi pubblicare il codice esatto? Non dovrebbe esserci un problema. – mlinuxgada

risposta

6

risolto utilizzando questo URL "http://192.168.0.20/" invece di "http://www.example.dev"

Anche il "host" intestazione è necessaria ...

$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

Probabilmente eseguite PHP su quel server Apache, e lì non avete la voce del file hosts.

+0

Il file hosts è su tutte le macchine (192.168.0.10 <= php cli, 192.168.0.20 <= apache) –

+0

@karoly, grazie! questo mi illumina! Stavo usando la finestra mobile per i test e il contenitore non aveva la voce sul file hosts! – mloureiro