0

I'm trying to make automatic register script with curl.

I have html form:

<form method="post" action="/register/submit" autocomplete="off" id="js-register-form">
<input type="hidden" name="__csrf_token" value="28030357" />
<input name="nick" type="text" size="15" maxlength="15" value="" class="form-control" />
<input name="pass" type="password" size="15" maxlength="15" class="form-control" />
<input name="sex" type="radio" value="V" /> vyras </label>
<input name="sex" type="radio" value="M" /> moteris </label>

<select name="age_day" class="form-control">
<option>Diena</option>
<option>1</option><option>2</option><option>3</option>                    
</select>

<select name="age_month" class="form-control">
<option>Mėnuo</option>
<option value="1">Sausis</option><option value="12">Gruodis</option></select>

<select name="age_year" class="form-control">
<option>Metai</option>
<option>2001</option><option>2000</option><option>1999</option>                   </select>

<input name="email" type="text" value="" size="15" maxlength="50" class="form-control" />

<select name="city_id" class="form-control"><option value="">Miestas</option><option value="1340915">Akmenė</option><option value="1341068">Alytus</option><option value="1341242">Anykščiai</option><option value="1341276">
</select>

<button type="submit" title="Registruotis" class="btn btn-signup js-register-form-submit">Registruotis</button>
</form>

and this is my curl script, but it doesn't work. What s wrong with it?:

<?php 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://login.com....html");
curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS,
            "__csrf_token=28030357&nick=pixelis98722&pass=qwerty&sex=V&age_day=12&age_month=04&age_year=2000&[email protected]&city_id=1343628"); 

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);

curl_close ($ch);

// further processing ....
if ($server_output == "OK") { echo"gerai"; } else { echo "bad"; }
?>

but doesn't work. Whats wrong with it?

4
  • what's the error, why do you think it doesn't work? Commented Jul 10, 2014 at 11:45
  • __csrf_token will ultimately change and is held in session serverside, so whatever value you use will not be valid for multiple requests. your need to scrape it everytime and use session persistence in the curl request. Commented Jul 10, 2014 at 11:45
  • __csrf_token doesn't chage. Commented Jul 10, 2014 at 11:47
  • have heard of ajax ? that may help you Commented Jul 10, 2014 at 12:06

1 Answer 1

1

use this

function httpPost($url,$params)
{
    $postData = '';
    //create name value pairs seperated by &
    foreach($params as $k => $v)
    {
        $postData .= $k . '='.$v.'&';
    }
    rtrim($postData, '&');

    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POST, count($postData));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

    $output=curl_exec($ch);

    curl_close($ch);
    return $output;

}

This is the final answer. Try anyone. Both works.

<?php
$a=$_POST['__csrf_token'];
$b=$_POST['nick'];
$c=$_POST['pass'];
$d=$_POST['sex'];
$e=$_POST['age_day'];
$f=$_POST['age_month'];
$g=$_POST['age_year'];
$h=$_POST['email'];
$i=$_POST['city_id'];



$url = 'http://login.com....html';
$postData = array();
$postData['a'] = $a;
$postData['b'] = $b;
$postData['c'] = $c;
$postData['d'] = $d;
$postData['e'] = $e;
$postData['f'] = $f;
$postData['g'] = $g;
$postData['h'] = $h;
$postData['i'] = $i;

$parameters=json_encode($postData);

$headers = array(    "Accept-Encoding: gzip",
    "Content-Type: application/json");


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
$result = curl_exec($ch);

//var_dump($result);
curl_close($ch);
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Have a look at http_build_query() to replace that loop
Did you try my answer?? @user2289809

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.