PHP cURL重定向到本地主机 [英] PHP cURL redirects to localhost

查看:207
本文介绍了PHP cURL重定向到本地主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有cURL的php脚本登录到外部网页.我是cURL的新手,所以感觉好像缺少很多东西.我找到了一些示例,并对其进行了修改,以允许访问https页面.最终,我的目标是能够登录到页面并通过登录后遵循指定的链接下载.csv.脚本如下所示:

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

即使CURLOPT_RETURNTRANSFER和CURLOPT_FOLLOWLOCATION都设置为True.

even though CURLOPT_RETURNTRANSFER and CURLOPT_FOLLOWLOCATION are both set to True.

第二,该页面的URL停留在"localhost/folderName/test.php",而不是定向到实际的网站.谁能解释为什么会这样?由于该脚本实际上并未重定向到已登录的网页,因此我似乎无法做任何我需要做的事情.

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.

我的问题与Cookie有关吗?我的cookies.txt文件与.php脚本位于同一文件夹中. (我正在使用wampServer btw).应该在其他地方吗?

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?

一旦我能够解决这两个问题,看来我所需要做的就是重定向到启动.csv文件下载过程的链接.

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.

非常感谢您的帮助!

推荐答案

回答部分问题:

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

CURLOPT_RETURNTRANSFER TRUE,以字符串形式返回传输 返回curl_exec()的值,而不是直接将其输出.

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

换句话说-完全按照您的描述进行操作.它会将响应返回到字符串,然后您echo即可查看它.根据要求...

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;

显示的页面地址更改为$website-在我的情况下,这是我用来存储与您的' https://www.websiteurl.com/login '

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'

我不确定这是您想要做的-因为我不确定我是否了解您的下一步.如果您是通过登录站点重定向的,那么新地址是否不属于返回的标头的一部分?并不需要提取该地址来执行下一个请求(wget或其他)来下载要获取的文件吗?

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?

为此,您需要将CURLOPT_HEADER设置为TRUE,

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

您可以从

$last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); 

(请参阅 cURL,将重定向URL获取到变量)

同一链接还具有一个有用的脚本,用于完全解析标头信息(在CURLOPT_HEADER==true时返回.这在nico limpica的回答中.)

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.

底线:如果您将CURL指向特定站点,它将获取您的浏览器将收到的信息;这并不意味着您的浏览器就好像您将其指向该站点一样...

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...

这篇关于PHP cURL重定向到本地主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆