卷曲登录并在会员区再次发布

时间:2012-03-30 08:47:22

标签: php curl

我正在使用curl自动登录会员区并且它已成功但在登录后我想在会员区内张贴一张表格,但我不知道该怎么做,你能帮帮我吗,我的目前的代码如下......

$main_url = 'https://www.url.com';

$redirect = '/reports/marks-checker';
$email =    'user@gmail.com'; 
$password = 'pass123'; 


$url= $main_url.'/account/login'; 
$cookie="cookie.txt"; 

$postdata = 'redirect='.$redirect.'&EmailAddress='.$email.'&Password='.$password.'&RememberMe=1'; 

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, true); 
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt ($ch, CURLOPT_REFERER, $url); 

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch); 
echo $result;

curl_close($ch);

上面的代码打印(echo)成员区域和表单(如下所示),我想查询...

<form method="POST">
<input type="radio" name="IDS" value="F" checked/>
<input type="radio" name="IDS" value="H"/>
<textarea name="q" cols="80" rows="15" wrap="none" style="width:99%"></textarea>
<select name="SortBy">
    <option value="0">rank</option>
    <option value="1">date</option>
</select>
<input type="submit" name="getmarks" value="Check marks"/>
</form>

我想将数据发布到此表单并获取结果,以便我可以使用正则表达式获取数据。感谢。

1 个答案:

答案 0 :(得分:1)

您需要使用DOM函数或正则表达式或表单的先验知识(适用于您的情况)来创建表单变量数组和要分配它们的值:

    $form_vars = array();

名称/值对IDS / F或H,q / some text,SortBy / 0或1.

    $form_vars[$var_name] = $value;

e.g。

    $form_vars['IDS'] = "H";

然后使用http_build_query函数构建您的查询:

    $encoded_form_vars = http_build_query($form_vars);

然后尝试这样的事情:

    function postForm($url, $fields, $referer='') {
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect:"));
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_REFERER, $referer);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            return curl_exec($ch);
    }

用这样的东西打电话:

    $result = postForm($url, $encoded_form_vars);