【问题标题】:Why the header function is not working after CURL call?为什么 CURL 调用后 header 函数不起作用?
【发布时间】:2016-01-12 11:57:02
【问题描述】:

以下是使用 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中)工作得非常好。

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

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

以下代码不起作用。 URL http://www.complexknot.com/login.php 未打开并出现空白页。这是我面临的问题。

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

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

请有人帮助我。

提前致谢。

【问题讨论】:

    标签: php url curl http-headers uri


    【解决方案1】:

    这是因为 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');
    

    【讨论】:

    • 感谢您的回答。您能告诉我应该在哪里添加此行吗?或者更好的是请您发布我的完整代码并添加新行作为答案,以便我接受您的答案?
    • 抱歉,问题依然存在。
    • 你可以这样尝试吗(再次查看我编辑的答案)。如果它不起作用,只需查看页面的来源以查看打印的内容..
    • 首先再次感谢您的帮助。你的伎俩对我有用,但我不能这样做。我想从我正在使用 CURL 调用的文件中重定向 URL,而不是从我正在从中进行 CURL 调用的文件中重定向 URL。如果 CURL 禁止这样做,那么您可以向我建议其他更好的解决方案,而不是使用 CURL。等待您的回复。
    • 使用$result = curl_exec($ch)); die($result)查看该页面返回了什么
    【解决方案2】:

    你可以使用

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 2014-01-26
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多