Pobieranie nagłówków wielu stron z curlem

0

Witajcie,

mam problem z pobieraniem nagłówków wielu stron w pętli. Nie jestem pewien czy to nie jest do końca problem konfiguracji serwera czy czegoś innego. Kod wygląda w ten sposób:

foreach ($text as $url) {
   $ch = curl_init($url); 
   curl_setopt($ch, CURLOPT_HEADER, 1); 
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_NOBODY, 1); 
				
   $return = curl_exec($ch); 

   var_dump($header);
}

Oczywiście wszystkie strony są na 100% dobre. URLe przekazuje formularzem w textarea. Co ciekawe poprawnie zwracane są nagłówki ostatniej strony na liście i to zawsze, natomiast wszystkie wcześniejsze są puste.
Curl zwraca bład: "Couldn't resolve host" jednak jeśli domena dla której wystąpił ten bład będzie na końcu na liście wszystko przejdzie OK. Czy to jest jakieś ograniczenie serwera?

0

Nie widzę, żeby zmienna $header była w ogóle definiowana gdziekolwiek.
Używaj tagów <code class="php"></code>.

0

Sorki,
zapomniałem dopisać:

foreach ($text as $url) {
   $ch = curl_init($url); 
   curl_setopt($ch, CURLOPT_HEADER, 1); 
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_NOBODY, 1); 
 
   $return = curl_exec($ch); 

   $header = explode("\n",$return);
   var_dump($header);
}
0

No i co jest źle?

$text = array(
    'http://onet.pl/404',
    'http://4programmers.net/404',
    'http://a.pl/'
);

foreach ($text as $url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 1);

    $return = curl_exec($ch);

    $header = explode("\n",$return);
    var_dump($header);
}
array (size=6)
  0 => string 'HTTP/1.1 301 Moved Permanently
' (length=31)
  1 => string 'location: http://www.onet.pl
' (length=29)
  2 => string 'server: edgeserver
' (length=19)
  3 => string 'Connection: keep-alive
' (length=23)
  4 => string '
' (length=1)
  5 => string '' (length=0)
array (size=11)
  0 => string 'HTTP/1.1 404 Not Found
' (length=23)
  1 => string 'Connection: close
' (length=18)
  2 => string 'Date: Wed, 25 Sep 2013 12:13:06 GMT
' (length=36)
  3 => string 'Server: Cherokee
' (length=17)
  4 => string 'X-Using-Wrapper: Yes
' (length=21)
  5 => string 'Set-Cookie: sid=a4c21dd747c3f; expires=Thu, 25-Sep-2014 18:01:52 GMT; path=/; domain=.4programmers.net; httponly
' (length=132)
  6 => string 'Set-Cookie: sid=a4c21dd74e517c3f; expires=Thu, 25-Sep-2014 18:01:52 GMT; path=/; domain=.4programmers.net; httponly
' (length=132)
  7 => string 'Content-type: text/html
' (length=24)
  8 => string 'X-Powered-By: chomiki
' (length=22)
  9 => string '
' (length=1)
  10 => string '' (length=0)
array (size=12)
  0 => string 'HTTP/1.1 200 OK
' (length=16)
  1 => string 'Cache-Control: private
' (length=23)
  2 => string 'Content-Length: 49334
' (length=22)
  3 => string 'Content-Type: text/html; charset=utf-8
' (length=39)
  4 => string 'Server: Microsoft-IIS/7.5
' (length=26)
  5 => string 'X-AspNetMvc-Version: 4.0
' (length=25)
  6 => string 'X-AspNet-Version: 4.0.30319
' (length=28)
  7 => string 'Set-Cookie: __Apl_Data=yowtaJxPZWw1Du6dlV1wnnsiyzz2IVpQ5rxjp_aZc6rVTO35kpEwu5tFXmiUaLzq0; expires=Thu, 25-Sep-2014 12:13:06 GMT; path=/
' (length=136)
  8 => string 'X-Powered-By: ASP.NET
' (length=22)
  9 => string 'Date: Wed, 25 Sep 2013 12:13:06 GMT
' (length=36)
  10 => string '
' (length=1)
  11 => string '' (length=0)
0

No to, że Curl zwraca błąd przy wszystkich domenach oprócz ostatniej w tablicy: Couldn't resolve host
Ja też tak testowałem, czyli wynika to że chyba to coś z konfiguracją serwera, tylko pytanie czy wiadomo co, może to można jakoś ustawić, zmienić.

0

A jak pojedynczo robisz to jest ok? A może to te domeny coś nie grają? A może masz bana jakiegoś?

0

Każda pojedynczo jest ok natomiast jak dam kilka na liście to ok jest tylko z tą która jest ostatnia na liście.

0

To ty lepiej zrób sobie var_dump na $text, bo moim zdaniem skopałeś tablicę wejściową, a szukasz problemów gdzie indziej

edit: a mój przykład odpalony poza czymkolwiek innym - działa Ci ok?

0
dzek69 napisał(a):

To ty lepiej zrób sobie var_dump na $text, bo moim zdaniem skopałeś tablicę wejściową, a szukasz problemów gdzie indziej

Miałeś racje.
Problem był taki że textarea explodowałem w ten sposób

explode('\n', $textarea);

, natomiast zadziałało tak: explode('\r\n', $textarea);


Dzieki za pomoc

1 użytkowników online, w tym zalogowanych: 0, gości: 1