带curl的多个动作 [英] multiple actions with curl
问题描述
我试图用curl做两个动作:
1.登录到管理页面
2.提交一个表单(添加用户)
第一个很好但第二个显示错误不存在。
这是我的代码:
I'm trying to do two actions with curl: 1. Login into admin page 2. Submit a form (add user) The first one go fine but the second show error as not loged in. Here is my code:
$ch1 = curl_init();
$ch2 = curl_init();
curl_setopt($ch1, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt($ch1, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch1, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch1, CURLOPT_URL, "http://admin.example.com/admin");
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS, "user=admin&pass=password");
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
curl_setopt($ch1, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch2, CURLOPT_URL, "http://admin.example.com/admin/adduser");
curl_setopt($ch2, CURLOPT_POST, 1);
curl_setopt($ch2, CURLOPT_POSTFIELDS, "newu=demo&pass=password");
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, 1);
$mh = curl_multi_init();
curl_multi_add_handle($mh, $ch1);
curl_multi_add_handle($mh, $ch2);
// execute all queries simultaneously, and continue when all are complete
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running);
//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
推荐答案
您可以使用相同的cURL处理。在这种情况下使用 curl_multi_exec
的问题是每个curl句柄都有不同的选项,而 $ ch2
You can perform both of these requests using the same cURL handle. The problem in using curl_multi_exec
in this case is that each curl handle has different options and $ch2
does not reference any cookies.
此外, curl_multi_exec
并行执行请求,这意味着您可以尝试在登录请求之前添加用户
Also, curl_multi_exec
performs the requests in parallel which means you may try to add the user before the login request is completed or even started.
请尝试此操作,它说明了使用 $ ch
登录,然后使用它再次添加用户。如果服务器支持 keep-alive
,那么可以添加 keep-alive
头,并且相同的套接字连接重新 - 用于第二个请求。
Try this instead, it illustrates logging in using $ch
, and then using it again to add the user. If the server supports keep-alive
, then you can add a keep-alive
header and the same socket connection is re-used for the second request.
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_URL, "http://admin.example.com/admin");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "user=admin&pass=password");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
$res = curl_exec($ch);
// check $res here to see if login was successful
curl_setopt($ch, CURLOPT_URL, "http://admin.example.com/admin/adduser");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "newu=demo&pass=password");
$res = curl_exec($ch);
// check $res to see that the user was successfully created
curl_close($ch);
这里有一些其他答案,显示如何在登录后使用cURL向同一网站发出多个串行请求。
使用PHP和Curl登录Google,Cookie已关闭?
检索Android Market mylibrary with curl
PHP Curl - Cookie问题
Here are some other answers showing how to make multiple serial requests to the same site using cURL after logging in.
Login to Google with PHP and Curl, Cookie turned off?
Retrieve Android Market mylibrary with curl
PHP Curl - Cookies problem
这篇关于带curl的多个动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!