-1

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.

PHPLover
  • 1
  • 51
  • 158
  • 311

2 Answers2

0

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');
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
  • Thanks for your answer. Could you please tell me where should I add this line or better would you please post my complete code with new line added as an answer so that I could accept your answer? – PHPLover Oct 14 '15 at 12:01
  • Sorry, but still the problem persists. – PHPLover Oct 14 '15 at 13:49
  • Can you try it like this (see my edited answer again). if it doesn't work just view the source of the page to see what is printed.. – Mihai Matei Oct 14 '15 at 13:54
  • First of all thanks once again for your help. Your trick worked for me but I can't do like this. I want to redirect the URL from the file which I'm calling using CURL and not from a file from which I'm giving CURL call. If CURL prohibits this then you can suggest me some other better solution rather than using CURL. Waiting for your reply. – PHPLover Oct 14 '15 at 13:59
  • use `$result = curl_exec($ch)); die($result)` to see what is that page returning – Mihai Matei Oct 14 '15 at 14:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92271/discussion-between-phplover-and-matei-mihai). – PHPLover Oct 14 '15 at 14:08
  • I really can't do it right now because I'm @work :) I will try help you later on.. thanks for understanding – Mihai Matei Oct 14 '15 at 14:12
  • yes sure, Thank you so much for the help you provided till now. – PHPLover Oct 14 '15 at 14:14
0

You can use

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

Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41
dinesh
  • 181
  • 2
  • 13