2011-09-27 6 views
6

Sunucuya bir yönlendirme bilgisi var ve sunucudan yanıt geldiğinde, 4XX ile başlayan herhangi bir kod varsa bir istisna atmak için HTTP kodunu kontrol etmek istiyorum. Bunun için başlıktan sadece HTTP kodunu nasıl alabilirim? Ayrıca burada sunucuya yönlendirme söz konusu. Bu yüzden curl bana yararlı olmayacağından korkuyorum.Yönlendirme sonrası HTTP yanıt kodu

Şimdiye kadar this solution'u denedim ancak çok yavaş ve davamda zaman aşımı oluşturuyor. Kod zaman aşımı süresini artırmak ve sadece bir HTTP kodu almak için daha uzun süre beklemek istemiyorum.

Herhangi bir öneriniz için şimdiden teşekkür ederiz. gibi

+2

cURL takip edebilirsiniz yönlendirmeleri gayet. Varsayımlarınızdan korkmak yerine belgede okuyabilirsiniz: http://php.net/manual/en/function.curl-setopt.php, CURLOPT_FOLLOWLOCATION'ı arayın – Piskvor

+0

CURLOPT_FOLLOWLOCATION seçeneği ile curl çalıştım. Thing, yerel sunucuma bir sayfa getirdi ve sunucudan aldığım sayfada kullanıcı adı/parola girdiğimde, sunucu yeniden yönlendirme konusunda kafa karıştırıyor. Bunu nasıl çözebilirim? –

cevap

6

şey:

$ch = curl_init(); 
    $httpcode = curl_getinfo ($ch, CURLINFO_HTTP_CODE); 

Sen HttpEngine Class denemelisiniz. Bu yardımcı olur umarım.

-

DÜZENLEME

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_USERAGENT, $your_agent_variable); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_REFERER, $your_referer); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
$output = curl_exec($ch); 
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
curl_close($ch); 

if ($httpcode ...) 
+0

Üzgünüm HttpEngine sınıfının farkında değilim. Bana biraz bilgi verebilir misin? –

+0

Sorun değil. Peki, bu sınıfa nereden geldiğimi hatırlamıyorum ... Bu yüzden buraya bir tane yükledim: http://pastebin.ca/2083612 Herhangi bir yardıma ihtiyacın olursa lütfen! –

+0

Sınıf dosyasına baktım ama burada nasıl yardımcı olacağından emin değilim. Yönlendirme için CURLOPT_FOLLOWLOCATION değerini true yaptığımda curl ve curl kullanarak, GET yöntemi tüm sayfayı getiriyor. –

0

Bulduğunuz çözüm iyi görünüyor. Sunucu size http başlıklarını zamanında gönderemiyorsa, probleminiz diğer sunucunun bozuk veya çok ağır yük altında olmasıdır.

8

Sizin get_headers ile yöntem ve daha da önemlisi (varsa) ve yönlendirme durum kodu dönecektir ilk tepki hattını isteyerek, dosyanın tamamını transfer edecek bir GET isteği yapacağız.

Sadece HEAD isteğine ve daha sonra üstbilgilerin ayrıştırılmasına ve son durum kodunu döndürmesine gerek vardır. bunu yapan bir kod örneği aşağıda verilmiştir, bunun yerine get_headers ait $http_response_header kullanıyor, ancak dizinin formatı aynıdır: Daha fazla bilgi için

$url = 'http://example.com/'; 

$options['http'] = array(
    'method' => "HEAD", 
    'ignore_errors' => 1, 
); 

$context = stream_context_create($options); 

$body = file_get_contents($url, NULL, $context); 

$responses = parse_http_response_header($http_response_header); 

$code = $responses[0]['status']['code']; // last status code 

echo "Status code (after all redirects): $code<br>\n"; 

$number = count($responses); 

$redirects = $number - 1; 

echo "Number of responses: $number ($redirects Redirect(s))<br>\n"; 

if ($redirects) 
{ 
    $from = $url; 

    foreach (array_reverse($responses) as $response) 
    { 
     if (!isset($response['fields']['LOCATION'])) 
      break; 
     $location = $response['fields']['LOCATION']; 
     $code = $response['status']['code']; 

     echo " * $from -- $code --> $location<br>\n"; 
     $from = $location; 
    } 
    echo "<br>\n"; 
} 

/** 
* parse_http_response_header 
* 
* @param array $headers as in $http_response_header 
* @return array status and headers grouped by response, last first 
*/ 
function parse_http_response_header(array $headers) 
{ 
    $responses = array(); 
    $buffer = NULL; 
    foreach ($headers as $header) 
    { 
     if ('HTTP/' === substr($header, 0, 5)) 
     { 
      // add buffer on top of all responses 
      if ($buffer) array_unshift($responses, $buffer); 
      $buffer = array(); 

      list($version, $code, $phrase) = explode(' ', $header, 3) + array('', FALSE, ''); 

      $buffer['status'] = array(
       'line' => $header, 
       'version' => $version, 
       'code' => (int) $code, 
       'phrase' => $phrase 
      ); 
      $fields = &$buffer['fields']; 
      $fields = array(); 
      continue; 
     } 
     list($name, $value) = explode(': ', $header, 2) + array('', ''); 
     // header-names are case insensitive 
     $name = strtoupper($name); 
     // values of multiple fields with the same name are normalized into 
     // a comma separated list (HTTP/1.0+1.1) 
     if (isset($fields[$name])) 
     { 
      $value = $fields[$name].','.$value; 
     } 
     $fields[$name] = $value; 
    } 
    unset($fields); // remove reference 
    array_unshift($responses, $buffer); 

    return $responses; 
} 

bkz: HEAD first with PHP Streams, bu örnek kodunu içeren sonunda nasıl yapabilirsiniz HEAD isteğini de get_headers ile yapın.

İlgili: How can one check to see if a remote file exists using PHP?

+0

İlgili: [PHP: get_headers geçici stream_context ayarla] (http://stackoverflow.com/questions/8429342/php-get-headers-set-temporary-stream-context) – hakre