[PHP][Curl] 取得302轉址後的url

PHP 利用 curl 取得轉址後的 url
function curl_302($url) {
    $ch = curl_init();
    curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch , CURLOPT_URL, $url);
    curl_setopt($ch , CURLOPT_FOLLOWLOCATION, 1); // 302 redirect
    $data  = curl_exec($ch); // return page content
    $info  = curl_getinfo($ch);
    $error = curl_errno($ch);
    curl_close($ch);
    
    if (!$error) 
    {
     return  $info["url"];
    } else {
     return FALSE;
    }
}
PS.一定要先執行curl_exec() 才可執行 curl_getinfo() 或 curl_errno()
curl_getinfo($ch) 內容
Array
(
 [url]                     => http://www.xxxx.com.tw;
 [content_type]            => text/html; charset=UTF-8
 [http_code]               => 200
 [header_size]             => 222
 [request_size]            => 280
 [filetime]                => -1
 [ssl_verify_result]       => 0
 [redirect_count]          => 0
 [total_time]              => 0.713066
 [namelookup_time]         => 0.003118
 [connect_time]            => 0.00621
 [pretransfer_time]        => 0.006214
 [size_upload]             => 0
 [size_download]           => 168710
 [speed_download]          => 236598
 [speed_upload]            => 0
 [download_content_length] => -1
 [upload_content_length]   => 0
 [starttransfer_time]      => 0.691713
 [redirect_time]           => 0
)

留言

這個網誌中的熱門文章

[MAC] MacBook Air 2012 手動換硬碟

[PHP] 將陣列中重複的值刪除最好方法 array_flip (移除陣列中重複的值)

[Linux] 看懂 Linux Memory Usage 記憶體使用率