2012-02-14 24 views
9

Mam kłopot z tym, że biblioteka PHP cURL rozpoznaje alias, który utworzyłem w moim pliku /etc/hosts.PHP cURL nie widzi pliku/etc/hosts

To jest to, co mam w moim pliku /etc/hosts teraz:

192.168.0.20 www.example.dev 

Z drugiej strony (192.168.0.20) Apache jest skonfigurowany do uruchomienia wirtualnego hosta w domenie example.dev. Alias ​​działa, jeśli przetestuję go w mojej przeglądarce, ale z PHP cURL po prostu nie działa.

Plik hosts po obu maszyn (192.168.0.10 < = PHP CLI 192.168.0.20 < = Apache).

Ze względu na kompletność jest to kod PHP, którego używam.

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

Uwaga: Ten problem wydaje się this one ale PHP związanych.

+0

Czy zrestartowałeś serwer? Tylko żeby się upewnić? –

+2

to plik hosts na właściwym komputerze? czy uruchamiasz PHP na tym komputerze? –

+0

Czy możesz podać dokładny kod? Nie powinno być problemu. – mlinuxgada

Odpowiedz

6

rozwiązać za pomocą tego URL "http://192.168.0.20/" zamiast "http://www.example.dev"

również "Host" jest potrzebne nagłówek ...

$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

Prawdopodobnie uruchamiasz PHP na tym serwerze apache, a tam nie masz wpisu pliku hosts.

+0

Plik hosts znajduje się na wszystkich komputerach (192.168.0.10 <= php cli, 192.168.0.20 <= apache) –

+0

@karoly dzięki! to mnie oświeca! Używałem dockera do testów, a kontener nie miał wpisu na pliku hosts! – mloureiro