curl 访问需要cookie的地址

    //如果访问地址是自身的话直接获取$_COOKIE组装一下然后发送
    $v) {
        $cookie .= $k."=".$v.";";
    }
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,0);
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $html = curl_exec($ch);
    var_dump($html);
    curl_close($ch);

    //如果访问的是其他站点的地址
    //首先获取远程站点cookie
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($curl, CURLOPT_HEADER, 0); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);  
    curl_setopt($curl, CURLOPT_COOKIEJAR, "cookiefile");
    curl_setopt($curl, CURLOPT_URL, $url1);
    $html = curl_exec($curl); 
    //然后带着cookie请求远程站点
    curl_setopt($curl, CURLOPT_COOKIEFILE, "cookiefile");
    curl_setopt($curl, CURLOPT_URL, $url2);
    $html = curl_exec($curl); 
    var_dump($html);
    curl_close ($curl); 
    ?>

你可能感兴趣的:(cookie,curl,php)