【问题标题】:I'm not able to redirect to an external website after curl requestscurl 请求后我无法重定向到外部网站
【发布时间】:2018-05-27 06:41:32
【问题描述】:

我成功地对外部网站进行了一些 curl 调用,事实上,在最后一次调用之后,我能够看到我希望看到的网站页面。唯一的问题:我只能将此页面视为 html 响应,保留在本地主机中,并且网站页面不可导航。 这篇文章 (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')

我得到了错误,而不是最后一次 curl 调用

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

因为与之前的 curl_exec() 存在“冲突”,虽然我关闭了它

【问题讨论】:

    标签: php redirect curl https


    【解决方案1】:

    这是我得出的结论:我想做的事情是不可能的,因为我需要为每个 curl 请求使用 cookie。 所以我不能从本地主机“出去”并登陆外部网站来导航它,因为在我退出的那一刻,我的 cookie 无效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-18
      • 2015-10-04
      • 2014-07-23
      • 2021-09-07
      • 2020-05-13
      • 2016-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多