简体   繁体   English

PHP cURL-POST是否发送到空白页?

[英]PHP cURL - POST is being sent to an empty page?

I'm trying to automate the usage of this site , where users can use temporary E-Mail addresses which can also be specified by them manually. 我正在尝试自动使用此网站 ,用户可以在其中使用临时的电子邮件地址,这些地址也可以由他们手动指定。 It uses a very simple anti-spam protection by having this <input name="csrf" type="hidden"> input inside the form with a randomized set of characters, which then needs to be included in the POST request. 它使用非常简单的反垃圾邮件保护,方法是在表单内部<input name="csrf" type="hidden"><input name="csrf" type="hidden">输入,并使用一组随机字符,然后将其包含在POST请求中。 So, if the CSRF is bj152nvua2ob , and I want my new address to be "john@l0real.net", my POST needs to be: 因此,如果CSRF为bj152nvua2ob ,并且我希望我的新地址为“ john@l0real.net”,则我的POST必须为:

csrf=bj152nvua2ob&mail=john&domain=@l0real.net

Problem is, I can't do this with cURL and PHP. 问题是,我无法使用cURL和PHP做到这一点。 Here's the top of my code: 这是我的代码的顶部:

$ch = curl_init ();

curl_setopt ($ch, CURLOPT_URL, "temp-mail.org/en/option/change/");
// Without CURLOPT_FOLLOWLOCATION, the page is not going to load.
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");

$html = curl_exec ($ch);

Then, after I've got the response, I start generating a new mail address and start getting the CSRF, therefore I've written two simple functions called "generate_mail" and "get_csrf" . 然后,在获得响应后,我开始生成一个新的邮件地址并开始获取CSRF,因此,我编写了两个简单的函数,分别称为“ generate_mail”和“ get_csrf” I've tested both of them, and they seem to work without any issues. 我已经测试了它们两个,它们似乎可以正常工作。

$csrf = get_csrf ($html);
$mail = generate_mail ();
$post = "csrf=$csrf&mail=$mail&domain=@l0real.net";
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);

file_put_contents ("final_response.html", curl_exec ($ch));

curl_close ($ch);

After creating the file final_response.html on my PC and then viewing it, I've noticed that the results aren't what I've expected. 在我的PC上创建文件final_response.html并查看它之后,我注意到结果不是我所期望的。 Some tries later, I've decided to debug my connection using Fiddler, and then I've noticed something interesting. 经过一些尝试,我决定使用Fiddler调试连接,然后发现一些有趣的事情。 This is what I get when I use this site with my browser: 这是通过浏览器使用此网站时得到的:
用我的浏览器

and this with cURL: 这与cURL:
与cURL .

Notice how there is 1 element (excluding the tunnels) against... 4! 请注意,针对... 4的元素有多少(不包括隧道)! First element is an empty response, but the second one contains the HTML page. 第一个元素为空响应,但第二个元素包含HTML页面。 Last two are the same: third is an empty response (and that's where my POST was sent), and forth contains the HTML page with different CSRF and incorrect mail address (the one that was generated by the site itself, not by me). 最后两个是相同的:第三个是空响应(那是我的POST发送的地方),第四个包含具有不同CSRF和不正确邮件地址的HTML页面(该页面是由网站本身生成的,而不是由我生成的)。 From what I can tell, each time I use curl_exec , it first loads an empty page, but then loads the correct one. 据我所知,每次使用curl_exec ,它首先加载一个空页面,然后加载正确的页面。 All of my requests are being sent to the empty one, thus being ignored later on. 我的所有请求都被发送到空请求,因此稍后将被忽略。 Is this a security measure, or I didn't configure cURL correctly? 这是安全措施,还是我没有正确配置cURL? I've tried to provide as much information as I could, showcasing each, and every step of mine, hoping that this problem can be fixed. 我已尝试提供尽可能多的信息,展示了我的每一个步骤,希望可以解决此问题。

Solved by adding these lines of code: 通过添加以下代码行来解决:

curl_setopt ($ch, CURLOPT_POSTREDIR, 3);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $path_to_cookies);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $path_to_cookies);

Now, it works the way intended! 现在,它可以按预期的方式运行!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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