Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

php - Why the header function is not working after CURL call?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...