I have the following code and when i try to get a value from the array $headers nothing appears, but when I use var_dump($headers) it shows all the array values. What Am I doing wrong?
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');
Output
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
"
}
var_dump($headers)?CURLOPT_FOLLOWLOCATIONto false. It's following the redirect, so when cURL's done, the headers may be from that page instead.