我发现curl请求后无法重定向到外部网站

时间:2017-12-13 11:29:48

标签: php redirect curl https

我成功地对外部网站进行了一些卷曲调用,事实上在最后一次之后,我能够看到我希望看到的网站页面。唯一的问题是:我只能将此页面视为html响应,保留在localhost中,并且网站页面无法导航。 这篇文章(PHP cURL redirects to localhost)讨论了一个类似的问题,虽然解决方案似乎不适合我。

特别是,这是我的代码:

<?php 

$products = explode(',', $_GET['products']);
$reqheaders = [
    'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'Accept-Encoding:gzip, deflate, br',
    'Accept-Language:it',
    'Connection:keep-alive',
    'Host:www.clubpervoi.com',
    'Referer:https://www.website.com/',
    'Upgrade-Insecure-Requests:1',
    'User-Agent:'. $_SERVER['HTTP_USER_AGENT']
];
$cookie_file = dirname(__FILE__) . '/cookie.txt';


$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://www.website.com/');
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($curl, CURLOPT_COOKIESESSION, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_exec($curl);
curl_close($curl);


// Action calls
for($i=0; $i<3; $i++) {
    $ch = curl_init();
    $params = array('code'=> $products[$i]);
    $query = http_build_query($params);
    try{
        curl_setopt($ch, CURLOPT_URL, 'https://www.website.com/foo/addProduct?'.$query);
        //curl_setopt($ch, CURLOPT_HEADER, 1);
        //curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $reqheaders);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
        curl_setopt($ch, CURLOPT_COOKIESESSION, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
        $resp = curl_exec($ch);
        curl_close($ch);

    } catch (Exception $e) {
        echo $e;
    }
}


//Here I expect to be redirected to the summary page
try{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.website.com/foo/summary');
    //curl_setopt($ch, CURLOPT_HEADER, 1);
    //curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $reqheaders);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
    curl_setopt($ch, CURLOPT_COOKIESESSION, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
    curl_exec($ch);
    curl_close($ch);

} catch (Exception $e) {
    echo $e;
}

正如你在上次打电话中看到的那样,我设定了

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);

理论上它可以解决问题,但不是我的情况。

显然,如果我尝试使用

header('location https://www.website.com/foo/summary')

而不是最后一次卷曲调用我得到了错误

Warning: Cannot modify header information - headers already sent by...

因为存在&#34;冲突&#34;使用之前的curl_exec(),虽然我关闭了它

1 个答案:

答案 0 :(得分:0)

这是我得出的结论:我想做的事情是不可能的,因为我需要为每个卷曲请求使用cookie。 所以我不能出去&#34;从localhost登陆并登陆外部网站进行导航,因为在我退出的那一刻,我的cookie无效。