CURL调用后为什么标头功能不起作用?

时间:2015-10-14 11:54:42

标签: php url curl http-headers uri

以下是使用CURL调用URL:

<?php
    ini_set('display_startup_errors',1);
    ini_set('display_errors',1);
    error_reporting(-1);

    $link = $_GET['link'];
    $url  = "http://www.complexknot.com/user/verify/link_".$link."/";


    // create a new cURL resource
    $ch = curl_init();

    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    // grab URL and pass it to the browser
    curl_exec($ch);

    // close cURL resource, and free up system resources
    curl_close($ch);
?>

变量$url包含一个我使用CURL命中的网址。

文件中写入的逻辑(存在于变量$url中)工作正常。

执行代码后,我希望将控件重定向到一个URL。为此,我写了以下代码:

header('Location: http://www.complexknot.com/login.php');
exit; 

以下代码无效。网址http://www.complexknot.com/login.php未打开,系统会显示空白页面。这是我面临的问题。

如果我没有使用CURL并点击网址,即$url中包含的网址,那么它会重定向到网址http://www.complexknot.com/login.php,这意味着当我点击网址时标题功能正常工作在浏览器中。

当我从CURL调用它时,为什么它不起作用?

请有人帮帮我。

提前致谢。

2 个答案:

答案 0 :(得分:0)

这是因为CURL正在输出数据。您必须使用curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);才能让CURL返回数据而不是输出数据。

<?php
ini_set('display_startup_errors', 0);
ini_set('display_errors', 0);
error_reporting(0);

$link = $_GET['link'];
$url  = "http://www.complexknot.com/user/verify/link_$link/";

// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

header('Location: http://www.complexknot.com/login.php');

答案 1 :(得分:0)

您可以使用

<?php echo "<script>window.location.href = 'http://www.complexknot.com/login.php';</script>";die; ?>