Why the header function is not working after CURL call?

自古美人都是妖i 提交于 2019-12-13 08:09:02

问题


Following is the call to an URL using CURL :

<?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);
?>

The variable $url contains one URL which I'm hitting using CURL.

The logic written in the file(present in a variable $url) is working absolutely fine.

After executing the code I want the control to be redirected to one URL. For it I've written following code :

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

The following code is not working. The URL http://www.complexknot.com/login.php is not opening and a blank white page appears. This is the issue I'm facing.

If I don't use the CURL and hit the URL i.e. the URL contained in $url then it gets redirect to the URL http://www.complexknot.com/login.php that means header function works fine when I hit the URL in browser.

Why it's not working when I call it from CURL?

Please someone help me.

Thanks in advance.


回答1:


This is happening because CURL is outputting the data. You must use curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); in order to let CURL returning data instead of outputting it.

<?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');



回答2:


You can use

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



来源:https://stackoverflow.com/questions/33124591/why-the-header-function-is-not-working-after-curl-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!