curl

  1.  
  2. $url = 'http://c.b1za.com/h.2SXXls?cv=nXP87fAkEx&sm=0d941d';
    
    $curl = curl_init();
    
    curl_setopt($curl, CURLOPT_URL, $url);
    
    curl_setopt($curl, CURLOPT_HEADER, 1);
    
    curl_setopt($curl, CURLOPT_NOBODY,1);
    
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    
    $content=curl_exec($curl);
    
    $headerStr=curl_getinfo($curl);
    
    curl_close($curl);
    
    echo '<pre>';
    
    print_r($headerStr);
    
    Array
    
    (
    
       [url] => http://c.b1za.com/h.2SXXls?cv=nXP87fAkEx&sm=0d941d
    
       [content_type] => text/html;charset=UTF-8
    
       [http_code] => 200
    
       [header_size] => 175
    
       [request_size] => 82
    
       [filetime] => -1
    
       [ssl_verify_result] => 0
    
       [redirect_count] => 0
    
       [total_time] => 0.120233
    
       [namelookup_time] => 0.037243
    
       [connect_time] => 0.077366
    
       [pretransfer_time] => 0.077384
    
       [size_upload] => 0
    
       [size_download] => 0
    
       [speed_download] => 0
    
       [speed_upload] => 0
    
       [download_content_length] => 0
    
       [upload_content_length] => 0
    
       [starttransfer_time] => 0.120141
    
       [redirect_time] => 0
    
       [redirect_url] =>
    
       [primary_ip] => 106.11.62.91
    
       [certinfo] => Array
    
           (
    
           )
    
    )

file_get_contents

  1.  
$html = file_get_contents($url);

preg_match('/var url = \'(.+?)\'/', $html, $urls);

print_r($urls);

Array

(

   [0] => var url = 'http://a.m.taobao.com/i37424802695.htm?price=288&sourceType=item&suid=8be20505-0a16-4863-8c8c-1910b7373a3b&ut_sk=1.U%2B2FehgzDrwDAJm7pzfsyyn8_12278902_1485154518051.Copy.1&un=133fdc823ca48dcfd9378eee1c005948&share_crt_v=1&cpp=1&shareurl=true&spm=a313p.22.352.22903886283&short_name=h.2SXXls'

   [1] => http://a.m.taobao.com/i37424802695.htm?price=288&sourceType=item&suid=8be20505-0a16-4863-8c8c-1910b7373a3b&ut_sk=1.U%2B2FehgzDrwDAJm7pzfsyyn8_12278902_1485154518051.Copy.1&un=133fdc823ca48dcfd9378eee1c005948&share_crt_v=1&cpp=1&shareurl=true&spm=a313p.22.352.22903886283&short_name=h.2SXXls

)

$url = $urls[1];

$html = file_get_contents($url, false, stream_context_create(array ('http' => array ('follow_location' => false))));

//http://php.net/manual/zh/reserved.variables.httpresponseheader.php

//$http_response_header 数组与 get_headers() 函数类似。当使用HTTP 包装器时,$http_response_header 将会被 HTTP 响应头信息填充。$http_response_header 将被创建于局部作用域中。

print_r($http_response_header);

Array

(

   [0] => HTTP/1.1 301 Moved Permanently

   [1] => Server: nginx

   [2] => Date: Sat, 19 Aug 2017 12:01:18 GMT

   [3] => Content-Type: text/html

   [4] => Content-Length: 280

   [5] => Connection: close

   [6] => Location: http://item.taobao.com/item.htm?id=37424802695&price=288&sourceType=item&suid=8be20505-0a16-4863-8c8c-1910b7373a3b&ut_sk=1.U%2B2FehgzDrwDAJm7pzfsyyn8_12278902_1485154518051.Copy.1&un=133fdc823ca48dcfd9378eee1c005948&share_crt_v=1&cpp=1&shareurl=true&spm=a313p.22.352.22903886283&short_name=h.2SXXls

)

preg_match('#Location:(.*)#',$http_response_header[6],$m);

print_r($m);

Array

(

   [0] => Location: http://item.taobao.com/item.htm?id=37424802695&price=288&sourceType=item&suid=8be20505-0a16-4863-8c8c-1910b7373a3b&ut_sk=1.U%2B2FehgzDrwDAJm7pzfsyyn8_12278902_1485154518051.Copy.1&un=133fdc823ca48dcfd9378eee1c005948&share_crt_v=1&cpp=1&shareurl=true&spm=a313p.22.352.22903886283&short_name=h.2SXXls

   [1] =>  http://item.taobao.com/item.htm?id=37424802695&price=288&sourceType=item&suid=8be20505-0a16-4863-8c8c-1910b7373a3b&ut_sk=1.U%2B2FehgzDrwDAJm7pzfsyyn8_12278902_1485154518051.Copy.1&un=133fdc823ca48dcfd9378eee1c005948&share_crt_v=1&cpp=1&shareurl=true&spm=a313p.22.352.22903886283&short_name=h.2SXXls

)