PHP cURL POST follow location

Using cURL to get the last URL address of POSTED is simply add this line to the cURL request:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

To know the last url from post request, after curl_exec, add this line:

curl_exec($ch);
$url = curl_getinfo($ch , CURLINFO_EFFECTIVE_URL);
echo $url;

Full sxample:

$url = 'http://localhost/images/140011917409pdcin_tmb.jpg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
//don't download content, false
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

$data = curl_exec($ch);
//var_dump($data);

/* Check for 404 (file not found). */
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($httpCode == 404)
{
	$status = false;
}
else
{
	$status = true;
}

/* find last url of post*/
$url = curl_getinfo($ch , CURLINFO_EFFECTIVE_URL);
$url_pieces = explode('?', $url);
/* example of process last url */
switch ($url_pieces[0])
{
	case "http://localhost/notactive.html":
		$status = false;
		break;
	case "http://localhost/linkerror.html":
		$status = false;
		break;
	default:
		$status = true;
		break;
}
//print_r($url);

curl_close($ch);

By Keenlio, May 20, 2014

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *


4 − = two

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>