如何在PHP中使用cURL发送post请求后获取/重定向到下一页?

如何在PHP中使用cURL发送post请求后获取/重定向到下一页?,php,curl,Php,Curl,下面给出的代码正在发送正确的post请求。问题是,每当is分别为uname和pword分配不正确的用户名和密码时,is都会显示正确的输出,即“singin1.php”页面,并显示消息“错误的用户名或密码存储在$result中,但当提供正确的用户名和密码时,它会显示相同的“signin1.php”页面。它不会显示登录后应获得的授权屏幕 <?php //set POST variables $url = 'http://computerinfo.in/school/signin1.php';

下面给出的代码正在发送正确的post请求。问题是,每当is分别为uname和pword分配不正确的用户名和密码时,is都会显示正确的输出,即“singin1.php”页面,并显示消息“错误的用户名或密码存储在$result中,但当提供正确的用户名和密码时,它会显示相同的“signin1.php”页面。它不会显示登录后应获得的授权屏幕

<?php
//set POST variables
$url = 'http://computerinfo.in/school/signin1.php';

$fields = array(
                        'uname' => 'username',
            'pword' => 'password',
            'submt' => 'Submit'
                );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string[]= $key.'='.$value;}
$fields_items = implode ('&', $fields_string);


//open connection
$ch = curl_init();
//fname%3Dasdf%26lname%3Dsdafasdf%26lolz%3DSubmit
//set the url, number of POST vars, POST data

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_items);

//execute post
$result = curl_exec($ch);
echo "<br>Field Item= "."<br>";
echo $result."=Result"."</br>";


echo curl_errno($ch) . '<br>' . 
            curl_error($ch);

//close connection
curl_close($ch);

?>
如果我手动提供用户名和密码,signin.php工作正常

因为“singin1.php”页面正在使用带有标题和会话的重定向。必须告诉cURL遵循重定向并获取cookie

要遵循重定向,应添加此行

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
和获取cookies并创建会话

curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE,  'cookie.txt');
test.php的最终代码如下所示

<?php
//set POST variables
$url = 'http://computerinfo.in/school/signin1.php';

$fields = array(
                        'uname' => 'username',
            'pword' => 'password',
            'submt' => 'Submit'
                );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string[]= $key.'='.$value;}
$fields_items = implode ('&', $fields_string);


//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //NEW LINE
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_items);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); //NEW LINE
curl_setopt($ch, CURLOPT_COOKIEFILE,  'cookie.txt'); // NEW LINE
//execute post
$result = curl_exec($ch);

echo "<br>Field Item= "."<br>";
echo $result."=Result"."</br>";


echo curl_errno($ch) . '<br>' . 
            curl_error($ch);
//close connection
curl_close($ch);

?>

代码可能依赖于会话。存储并使用会话cookie,发出初始请求以加载带有表单的页面,获取cookie,将其用于发布的数据。是的,我已经为signn1.php页面添加了抽象代码。。。。获取Cookie并将其与发布的数据一起使用。。。你能给出一个如何做的提示,或者任何一个教程。。。。我是卷发新手。感谢您使用标题和重定向;您必须告诉CURL遵循重定向。curl_setopt$ch,CURLOPT_FOLLOWLOCATION,true;,但是,为了使用会话,您必须存储cookies。太好了,现在我可以跟踪重定向。问题仍然存在:如何获取cookies,我可以将cookies存储在浏览器中,然后登录到系统吗?不确定为什么要将这些cookies存储在浏览器中。例如,要存储它们并与curl一起使用,请查看。
<?php
//set POST variables
$url = 'http://computerinfo.in/school/signin1.php';

$fields = array(
                        'uname' => 'username',
            'pword' => 'password',
            'submt' => 'Submit'
                );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string[]= $key.'='.$value;}
$fields_items = implode ('&', $fields_string);


//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //NEW LINE
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_items);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); //NEW LINE
curl_setopt($ch, CURLOPT_COOKIEFILE,  'cookie.txt'); // NEW LINE
//execute post
$result = curl_exec($ch);

echo "<br>Field Item= "."<br>";
echo $result."=Result"."</br>";


echo curl_errno($ch) . '<br>' . 
            curl_error($ch);
//close connection
curl_close($ch);

?>