user3305327 user3305327 - 3 months ago 16
PHP Question

find the redirected url using curl

I am trying to find the redirected url from a source url to get this I am using the following code...

$url="http://www.idealo.fr/go/737821809.html?categoryId=12313&pos=1&price=499.99&productid=4716350&sid=26246&type=offer";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

$header = curl_exec($ch);
$retVal = array();
$fields = explode("\r\n", preg_replace_callback ('/\x0D\x0A[\x09\x20]+/', ' ', $header));
foreach( $fields as $field ) {
if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
$match[1] = preg_replace_callback ('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
if( isset($retVal[$match[1]]) ) {
$retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
} else {
$retVal[$match[1]] = trim($match[2]);
}
}
}
echo '<pre>';
print_r($retVal);
echo '</pre>';

if (isset($retVal['Location'])){
echo $retVal['Location'];
} else {

echo $_GET[$urlKey];
}
curl_close($ch);


Now, it returns me the following output...

Array (
[date] => Tue, 06 Sep 2016 15:34:27 GMT
[server] => idealoAppServer
[location] => http://track.effiliation.com/servlet/effi.redir?id_compteur=13087834&url=http://www.priceminister.com/offer/buy/1134677256/canon-eos-750d-appareil-photo-numerique.html

[content-type] => text/html; charset=UTF-8
[content-length] => 0
[set-cookie] => Array
(
[0] => Array
(
[0] => oop_mvp_2=A; domain=.idealo.fr; path=/; expires=Thu, 05-Dec-2016 15:34:27 GMT
[1] => ipcuid=01jo0lb800isrmzsx0; domain=.idealo.fr; path=/; expires=Mon, 27-Aug-2018 15:34:27 GMT
)

[1] => icda=1; domain=.idealo.fr; path=/; expires=Wed, 06-Sep-2017 15:34:27 GMT
)

[vary] => Accept-Encoding,User-Agent
[connection] => close )


Now, from this array I just need the following output...

http://www.priceminister.com/offer/buy/1134677256/canon-eos-750d-appareil-photo-numerique.html


Can anyone please help me to form the array so that I just get the url only...I have recently upgraded to php7 from php 5...is that may be one of the reason...

Answer

You can use parse_url to parse the url you receive in location key

$url_parse = parse_url($retVal['location']);

After that you will have somehing like this in $url_parse:

array (
  'scheme' => 'http',
  'host' => 'track.effiliation.com',
  'path' => '/servlet/effi.redir',
  'query' => 'id_compteur=13087834&url=http://www.priceminister.com/offer/buy/1134677256/canon-eos-750d-appareil-photo-numerique.html',
)

So the query is in query key. Now we need to parse it and you cna use parse_str

parse_str($url_parse['query'], $output);

And now in $output you will have something like this:

array (
  'id_compteur' => '13087834',
  'url' => 'http://www.priceminister.com/offer/buy/1134677256/canon-eos-750d-appareil-photo-numerique.html',
)

So the url that you want is in $output['url']

echo $output['url']; //here is the url that you want.