2012-01-16 36 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)'的输出吗? – 2012-01-16 16:05:38

+0

您确定$ headers数组的“键”是整数吗? $ headers的print_r输出是什么样的(一​​个例子会很有用)。 – 2012-01-16 16:05:49

+0

好吧我添加了输出 – bammab 2012-01-16 16:12:34

回答

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 2012-01-16 16:22:22

0

当我跑机上的试验,报头大小只有7(包括两个空行)。你应该做的是在头文件的搜索行上寻找一个模式并采取行动。

例如:

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

     } 
    } 
} 

如果你想操纵头,一个好主意,可以建立与头的关联数组,这样就可以访问与协议名称的项目(如:$headers['location']) 。只需用':'爆炸标题,即可为该键应用strtolower。可能会有所帮助。

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结束。