2012-01-16 3 views
0

다음 코드를 가지고 있는데 배열에서 값을 가져 오려고하면 $headers 아무 것도 나타나지 않지만 var_dump($headers)을 사용하면 모든 배열 값이 표시됩니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 내 컴퓨터에서 테스트를 실행하면배열을 사용하여 PHP 함수에서 값을 얻는 방법

function linkcheck($url) { 
    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_HEADER, true); 
    curl_setopt($ch, CURLOPT_NOBODY, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    $headers = explode("\n", curl_exec($ch)); 
    curl_close($ch); 

    switch ($headers[18]) { 
     case "Location: https://somewebsite.com/welcome": 
      echo "Actice"; 
      break; 
     case "Location: https://somewebsite.com/no_such_link": 
      echo "Inactive"; 
      break; 

    } 
} 

echo linkcheck('http://somewebsite.com/54sdf'); 

출력

array(37) { 
    [0]=> 
    string(19) "HTTP/1.1 302 Found 
" 
    [1]=> 
    string(39) "Content-Type: text/html; charset=utf-8 
" 
    [2]=> 
    string(18) "Connection: close 
" 
    [3]=> 
    string(12) "Status: 302 
" 
    [4]=> 
    string(60) "X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 2.2.10 
" 
    [5]=> 
    string(34) "X-UA-Compatible: IE=Edge,chrome=1 
" 
    [6]=> 
    string(47) "Location: https://somewebsite.com/54sdf 
" 
    [7]=> 
    string(20) "X-Runtime: 0.006921 
" 
    [8]=> 
    string(132) "Set-Cookie: _sp_session_id=; domain=.superpoints.com; path=/; expires=Mon, 16-Jan-2012 18:08:29 GMT 
" 
    [9]=> 
    string(24) "Cache-Control: no-cache 
" 
    [10]=> 
    string(69) "Server: nginx/0.7.65 + Phusion Passenger 2.2.10 (mod_rails/mod_rack) 
" 
    [11]=> 
    string(1) " 
" 
    [12]=> 
    string(19) "HTTP/1.1 302 Found 
" 
    [13]=> 
    string(39) "Content-Type: text/html; charset=utf-8 
" 
    [14]=> 
    string(18) "Connection: close 
" 
    [15]=> 
    string(12) "Status: 302 
" 
    [16]=> 
    string(60) "X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 2.2.10 
" 
    [17]=> 
    string(34) "X-UA-Compatible: IE=Edge,chrome=1 
" 
    [18]=> 
    string(55) "Location: https://somewebsite.com/no_such_link 
" 
} 
+0

'var_dump ($ headers)'의 출력을 게시 할 수 있습니까? –

+0

$ headers 배열의 "keys"가 정수인지 확인 하시겠습니까? $ headers의 print_r 출력은 어떻게 생겼습니까 (예제가 유용 할 것입니다). –

+0

괜찮 았어. 내가 출력을 더했다. – bammab

답변

1

특정 위치에있는 특정 헤더를 사용하고 있습니다. 항상 그렇지는 않으며, HTTP 헤더 키는 대소 문자를 구분하지 않습니다.

이 시도 :

function linkcheck($url) { 

    // Do cURL 
    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_HEADER, true); 
    curl_setopt($ch, CURLOPT_NOBODY, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    $response = explode("\n", curl_exec($ch)); 
    curl_close($ch); 

    // Seperate headers from body 
    $parts = explode("\r\n\r\n", $response); 

    // Turn headers into associative array 
    $head = explode("\r\n", $parts[0]); 
    array_shift($head); // Skip response line 
    $headers = array(); 
    foreach ($head as $header) { 
     $header = explode(':', $header); 
     $key = strtolower(trim(array_shift($header))); 
     $val = trim(implode(':', $header)); 
     if (isset($headers[$key])) { 
      if (is_array($headers[$key])) { 
       $headers[$key][] = $val; 
      } else { 
       $headers[$key] = array($headers[$key], $val); 
      } 
     } else { 
      $headers[$key] = $val; 
     } 
    } 

    // If there is no location header, we can't test it 
    if (!isset($headers['location'])) { 
     echo "No location header"; 
     return; 
    } 

    switch ($headers['location']) { 
     case "https://somewebsite.com/welcome": 
      echo "Active"; 
      break; 
     case "https://somewebsite.com/no_such_link": 
      echo "Inactive"; 
      break; 
     default: 
      echo "Unknown value"; 
      break; 
    } 

} 
+0

정말 고마워요! 그냥 내가 필요하길 바래. – bammab

0

는 헤더 크기는 7 (두 개의 빈 줄 포함)이었다. 당신이해야 할 일은 헤더의 행을 따라 패턴을 찾고 행동을 취하는 것입니다.

예를 들어

: 당신이 헤더를 조작하려면

foreach ($headers AS $line) { 
    if (strpos('Location', $line)) { 
     list ($loc, $url) = explode(':', $line); 
     if ($url == 'keyword') {  
     // do stuff 

     } 
    } 
} 

것은 좋은 생각이 프로토콜 이름 항목에 액세스 할 수 있도록 헤더와 연관 배열을 구축 할 수있다 (예 : $headers['location']를) . ':'를 사용하여 머리글을 분해하면 키에 대한 견인기가 적용됩니다. 도움이 될 수 있습니다.

1

줄 바꿈 문제가있는 것 같습니다.

"Location: https://somewebsite.com/no_such_link 
" 

!= 

"Location: https://somewebsite.com/no_such_link" 

줄 바꿈/줄 바꿈을 제거하십시오.

1

당신은이 같은 스위치에 trim 기능을 사용하기 위해 필요한 것은 :

switch (trim($headers[18])) { 
    case "Location: https://somewebsite.com/welcome": 
     echo "Actice"; 
     break; 
    case "Location: https://somewebsite.com/no_such_link": 
     echo "Inactive"; 
     break; 
} 

이 때문에 당신의 헤더 배열의 모든 라인이에서 EOL이 얼굴 종료.

관련 문제