使用curl找到重定向的url

时间:2016-09-06 15:38:43

标签: php curl

我正在尝试从源网址找到重定向的网址以获取此信息我使用以下代码...

$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);

现在,它返回以下输出...

 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 )

现在,从这个数组我只需要以下输出......

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

任何人都可以帮我形成数组,以便我只获得网址...我最近从php 5升级到php7 ...这可能是其中一个原因...

1 个答案:

答案 0 :(得分:1)

您可以使用<Style TargetType="TextBox" x:Name="efTextBoxStyle"> <Setter Property="Template"> <Setter.Value> <ControlTemplate > <Border x:Name="border" BorderThickness="1" CornerRadius="2"> <ScrollViewer x:Name="PART_ContentHost"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsFocused" Value="True"> <Setter TargetName="border" Property="BorderBrush" Value="Red"/> <Setter Property="Background" Value="Blue"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> 来解析您在parse_url密钥中收到的网址

location

之后,您将在$url_parse = parse_url($retVal['location']);

中得到这样的信息
$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', ) 密钥中。现在我们需要解析它,你就可以使用query

parse_str

现在在parse_str($url_parse['query'], $output); 你会有这样的事情:

$output

所以你想要的网址是array ( 'id_compteur' => '13087834', 'url' => 'http://www.priceminister.com/offer/buy/1134677256/canon-eos-750d-appareil-photo-numerique.html', )

$output['url']