==== (Possible) Issue: Your PHP configuration has safe_mode
or open_basedir
enabled.
CURLOPT_FOLLOWLOCATION (integer) This constant is not available when
open_basedir or safe_mode are enabled.(http://php.net/manual/en/curl.constants.php)
==== (Possible) Issue: The remote service isn't responding as you expect. Break it down into individual parts and log the output, or check Google Chrome (or similar) for the redirect:
A-ha! Chrome shows that there is no redirect!
In PHP this might look something like the below. This code will cycle through the redirect chain manually and give you chance to inspect responses along the way.:
(see code below)
==== Issue: You are executing the request twice (you probably noticed this!):
$retValue = curl_exec($ch);
$response = json_decode(curl_exec($ch));
==== Issue: You are expecting to json_decode
a HTML response. This will not work (and can't be expected to).
IN SHORT
It looks like there is a redirect in JavaScript that this page is using, as opposed to normal header redirects. You might have to rethink your approach as you'll probably struggle to extract this information from the page, and it's certainly going to be subject to change. (It's actually submitting a form to the next URL so you'll have to work out where the data is from -- again, check the Chrome log).
(footnote) And the code that will help you spot this in PHP (for this URL it returns 200 straight away -- there is no redirect!):
<?php
$url = 'https://selfsolve.apple.com/agreementWarrantyDynamic.do?caller=sp&sn=990002316140324';
$http_headers = array(
'Accept: */*',
'Connection: keep-alive',
'Accept-Encoding:gzip, deflate, sdch',
'Accept-Language:en-US,en;q=0.8,es;q=0.6'
);
$finished = false;
$count = 0;//we don't want to redirect forever!
$currentUrl = $url;
while( $finished == false && $count < 10 ) {
$count++;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers);
curl_setopt($ch, CURLOPT_URL, $currentUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');
// not while we're testing: //curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
$retValue = curl_exec($ch);
$info = curl_getinfo($ch);
$responseCode = $info['http_code'];
if($responseCode > 300 && $responseCode < 303) {
echo "
redirecting ($responseCode) to ".$info['redirect_url'];
$currentUrl = $info['redirect_url'];
} else {
$finished = true;
echo "
finished ($responseCode) content length:".strlen($retValue);
}
}
//now try the whole thing
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
$retValue = curl_exec($ch);
$info = curl_getinfo($ch);
echo "
Whole request: finished ($responseCode) content length:".strlen($retValue). " total redirects:".$info['redirect_count'];
echo "
";
Output:
finished (200) content length:4833
Whole request: finished (200) content length:4833 total redirects:0