1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

cURL, get link behind redirect tutorial

Discussion in 'PHP & Perl' started by Mutikasa, Jan 29, 2012.

Tags:
  1. Mutikasa

    Mutikasa Power Member

    Joined:
    May 23, 2011
    Messages:
    575
    Likes Received:
    212
    If you have link1 that redirects to link2, but you need to get link2, this is the code
    PHP:
    function get_link2($link1) {
    $ch curl_init($link1);

    curl_setopt($chCURLOPT_HEADERtrue);
    curl_setopt($chCURLOPT_NOBODYtrue);
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

    $response curl_exec($ch);
    curl_close($ch);

    $header "Location: ";
    $pos strpos($response$header);
    $pos += strlen($header);
    $link2 substr($response$posstrpos($response"\r\n"$pos)-$pos);
    return 
    $link2; }