PHP file_get_contents () tarayıcıya farklı davranır

2 Cevap php

I'm trying to download the contents of a web page using PHP. When I issue the command:

$f = file_get_contents("http://mobile.mybustracker.co.uk/mobile.php?searchMode=2");

Bu sunucu aşağı olduğunu bildiriyor bir sayfa döndürür. Benim tarayıcınıza aynı yapıştırın zaman henüz beklenen sayfası olsun.

Herkes ne bu neden oluyor herhangi bir fikir var mı? File_get_contents bir tarayıcı isteğini ayırt herhangi başlıklarını aktarıyor mu?

2 Cevap

Evet, farklılıklar vardır - tarayıcı HTTP headers, derdim additionnal bol göndermek eğilimindedir; ve her iki tarafından gönderilen olanlar muhtemelen aynı değeri yok.

Burada, birkaç test yaptıktan sonra, it seems that passing the HTTP header called Accept gereklidir.

Bu additionnal bağlam bilgileri belirtmek için, file_get_contents üçüncü parametre kullanılarak yapılabilir:

$opts = array('http' =>
    array(
        'method'  => 'GET',
        //'user_agent '  => "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2) Gecko/20100301 Ubuntu/9.10 (karmic) Firefox/3.6",
        'header' => array(
            'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*\/*;q=0.8
'
        ), 
    )
);
$context  = stream_context_create($opts);

$f = file_get_contents("http://mobile.mybustracker.co.uk/mobile.php?searchMode=2", false, $context);
echo $f;

Bu ile, sayfanın HTML kodunu almak mümkün değilim.


Notes :

  • Ben ilk User-Agent geçen test, ama gerekli -- which is why the corresponding line is here as a comment olarak görünmüyor
  • The value is used for the Accept header is the one Firefox used when I requested that page with Firefox before trying with file_get_contents.
    • Diğer bazı değerler Tamam olabilir, ama ben gerekli biri olan değerini belirlemek için herhangi bir test yapmadım.


For more informations, you can take a look at :

% 20 ile tüm boşlukları değiştirmek