2012-04-02 77 views
1

我试图在我自己的网站上创建一个自动脚本,它登录到网站,通过一些POST头并基本上开始导出。CURL用旋转键提交表格

但是,我很难通过登录页面,因为每个页面加载都有一个不同的旋转键。

我试过运行脚本没有用,下面的脚本输出顶部的$ xid。但是,如果我检查$ xid回显它不是页面上当前的xid值。

编辑:好问题诺曼 - 这只是你的简单的隐藏字段,随机值在每次页面重新加载时都会改变。所以基本上,似乎我必须在'curl_exec'之前找到一个页面的xid,而我不知道该怎么做,或者甚至有可能。也许这需要一些JS和CURL。

EDIT2:Here is an example URL for the demo

任何想法,如何解决这个问题?

<?php 
set_time_limit(0); 

# Begin Header info 
$url = "https://secure.mywebsite.com/admin/import.php?mode=export"; 
$post = "mode=export&data%5yaddayaddayadda"; 
$agent = 'User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008100922 Ubuntu/8.04 (hardy) Firefox/3.0.3'; 
# End Header Info 

# Begin Processing Info 
$ch = curl_init($url); 
//curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
//curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie.txt'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
$content = curl_exec ($ch); 
curl_close ($ch); 
# End Processing Info 


# Begin finding xID 
$regex = '/name=\"xid\" value=\".*?\"/'; 
preg_match_all($regex,$content,$match); 
$xid = substr($match[0][0], 18, -1); 
echo $xid; 
# End finding xID 

    # Begin Header info 
    $url = "http://secure.mywebsite.com/admin/"; 
    $post = "username=myusernamehere&password=mypasswordhere&mode=login&usertype=P&xid=".$xid."&redirect=admin"; 
    $agent = 'User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008100922 Ubuntu/8.04 (hardy) Firefox/3.0.3'; 
    # End Header Info 

    # Begin Processing Info 
    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie.txt'); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    $content = curl_exec ($ch); 
    curl_close ($ch); 
    # End Processing Info 


# Begin connection to export file 
$url = "https://secure.mywebsite.com/admin/import.php?mode=export"; 
$post = "mode=export&data%5yaddayaddayadda"; 
# End connection to export file 

# Begin Export 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie.txt'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
$content = curl_exec ($ch); 
echo curl_exec($ch); 
curl_close ($ch); 
# End export 

?> 
+1

你是什么意思转动钥匙吗?如果存在令牌,就像我认为的那样,那么您可能需要存储随第一个请求一起提供的会话/ cookie,然后使用该令牌以及提交的所有其他详细信息。 – Norm 2012-04-02 23:11:40

+1

那么,你的curl请求正是一个post请求。我不确定表单在这方面做了什么。我的意思是减轻CSRF的意义是有意义的,这就是旋转键的作用。它看起来像你知道你在做什么,但我认为你可能需要从第一个请求存储cookie并重用它/(正如我在第一个评论中所说的)。否则,如果这个网站在线,请链接。 – Norm 2012-04-02 23:32:52

+1

一些建议:您还需要在每个请求中指定'CURLOPT_COOKIEFILE'。 COOKIEJAR是在您调用'curl_close'时写入cookie的地方,COOKIEFILE是在发出请求时从中读取cookie的地方。您也可以反复使用相同的卷曲手柄来减少代码。您只需更改每个请求的URL,方法和发布数据即可。我已经回答了几个类似的问题。看到[这个答案](http://stackoverflow.com/questions/9549892/multiple-actions-with-curl/9549990#9549990)链接到其他几个答案,展示你想做什么。 – drew010 2012-04-02 23:56:15

回答

1

第一个请求

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie.txt'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
$content = curl_exec ($ch); 
curl_close ($ch); 

二次要求:

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookie.txt'); 
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie.txt'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
$content = curl_exec ($ch); 
curl_close ($ch);