简体   繁体   中英

PHP cURL redirects to localhost

I'm trying to login to an external webpage using a php script with cURL. I'm new to cURL, so I feel like I'm missing a lot of pieces. I found a few examples and modified them to allow access to https pages. Ultimately, my goal is to be able to login to the page and download a .csv by following a specified link once logged in. So far, what I have is a script that tests logging in to the page; the script is shown below:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.websiteurl.com/login');
curl_setopt($ch, CURLOPT_POSTFIELDS,'Email='.urlencode($login_email).'&Password='.urlencode($login_pass).'&submit=1');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_setopt($ch, CURLOPT_REFERER, "https://www.websiteurl.com/login");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($ch);

I have a few questions. First, is there a reason this does not redirect on its own? The only way for me to view the contents of the page is to

echo $output

even though CURLOPT_RETURNTRANSFER and CURLOPT_FOLLOWLOCATION are both set to True.

Second, the URL for the page stays at "localhost/folderName/test.php" instead of directing to the actual website. Can anyone explain why this happens? Because the script doesn't actually redirect to a logged in webpage, I can't seem to do anything that I need to do.

Does my issue have to do with cookies? My cookies.txt file is in the same folder that my .php script is. (I'm using wampServer btw). Should it be located elsewhere?

Once I'm able to fix these two issues, it seems that all I need to be able to do is to redirect to the link that start the download process for the .csv file.

Thanks for any help, much appreciated!

Answering part of your question:

From http://php.net/manual/en/function.curl-setopt.php :

CURLOPT_RETURNTRANSFER TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

In other words - doing exactly what you described. It's returning the response to a string and you echo it to see it. As requested...

----- EDIT-----

As for the second part of your question - when I change the last three lines of the script to

$output = curl_exec($ch);
header('Location:'.$website);
echo $output;

The address of the page as displayed changes to $website - which in my case is the variable I use to store my equivalent of your ' https://www.websiteurl.com/login '

I am not sure that is what you wanted to do - because I'm not sure I understand what your next steps are. If you were getting redirected by the login site, wouldn't the new address be part of the header that is returned? And wouldn't you need to extract that address in order to perform the next request ( wget or whatever) in order to download the file you wanted to get?

To do so, you need to set CURLOPT_HEADER to TRUE,

You can get the URL where you ended up from

$last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); 

(see cURL , get redirect url to a variable ).

The same link also has a useful script for completely parsing the header information (returned when CURLOPT_HEADER==true . It's in the answer by nico limpica.

Bottom line: CURL gets the information that your browser would have received if you had pointed it to a particular site; that doesn't mean your browser behaves as though you pointed it to that site...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM