简体   繁体   English

PHP CURL-关注位置和发布字段问题

[英]PHP CURL - Issue with Follow Location and Post Fields

I'm having issues with following a redirect while maintaining session cookie and post field information. 我在保持会话cookie和发布字段信息的同时进行重定向时遇到问题。 This is how the process goes: 该过程如下:

1) Visit URL, they return a cookie and a 302 response (pointing to the same URL you just visited) 1)访问URL,它们返回一个cookie和302响应(指向您刚刚访问的URL)

2) Re-Visit URL with the cookie they gave you and you can see the proper page. 2)使用他们给您的cookie重新访问URL,您可以看到正确的页面。

I can get through to the proper page with CURLOPT_FOLLOWLOCATION = true , however I guess CURL doesn't keep the post fields when following a redirect, so there is no useful content on the page. 我可以使用CURLOPT_FOLLOWLOCATION = true来访问正确的页面,但是我想在重定向后CURL不会保留发布字段,因此页面上没有有用的内容。

I have tried manually storing the cookie, and performing the 'redirect' myself with the stored cookie, however with this method I never get past the 302 redirect to the proper page. 我尝试过手动存储cookie,并使用存储的cookie自己执行“重定向”,但是使用这种方法,我从不会通过302重定向到正确的页面。 The code for the manual method mentioned here is below. 此处提到的手动方法的代码如下。

$tmp_name = tempnam('tmp', 'COOKIE');
$url = "MY_URL";

$options = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_REFERER => $url,
    CURLOPT_HEADER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => array(
        'field1' => 'postfield1',
        'field2' => 'postfield2',
    ),
    CURLOPT_VERBOSE => true,
);

// Make the first request, specifying where to store the cookie
// This request returns the cookie and the 302 response
$ch = curl_init($url);
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_COOKIEJAR, $tmp_name);
$resp1 = curl_exec($ch);

// Make the second request, using the cookie stored above
// Should return the proper page, but gives me the 302 again instead.
$ch = curl_init($url);
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_COOKIEFILE, $tmp_name);
$resp2 = curl_exec($ch);

Does anyone know what's wrong with the above code, or if there's another way to accomplish the task? 有人知道上面的代码有什么问题吗,或者还有其他方法可以完成任务吗?

First of all, post data is never kept on redirect. 首先,发布数据永远不会保持重定向。 So don't worry about that, you don't have to make two requests. 因此,不必担心,您不必提出两个请求。 Just stick with 坚持下去

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

I would also suggest the following for further debuging: even if you make two requests, use same curl resource, don't close it to make new one. 我还建议您进行以下调试:即使您发出两个请求,使用相同的curl资源,也不要关闭它以发出新的请求。 Also, add: 另外,添加:

curl_setopt($ch, CURLOPT_FORBID_REUSE, 0);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 0);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "valid user agent");

You could also use browser addons (ie HttpFox) to check the exact cookies and requests sequence that are needed. 您还可以使用浏览器插件(即HttpFox)检查确切的cookie和请求所需的序列。 You are trying to emulate real request, so looking in-depth at one that your browser makes can help a lot. 您正在尝试模仿真实的请求,因此深入研究浏览器发出的请求会很有帮助。

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

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