Php Login To Remote Yii Site Via Curl

I would like to write script to login to my webhosting but I am getting “Hopla, page not found” page - it means I can’t login. What am I doing wrong?



function nacitaj_html($url)


    $ch = curl_init($url);   

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

    curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie0.txt'); 

    $content = curl_exec($ch);


    return $content;


$html = nacitaj_html(""); //echo $content; die();

$html = str_get_html($html);

$element = $html->find('input[name=YII_CSRF_TOKEN]');






echo $url.$element[0]->value; //die();


    'YII_CSRF_TOKEN' => urlencode($element[0]->value),

    'LoginForm[username]' => urlencode($username),

    'LoginForm[password]' => urlencode($password),

    'submit' => urlencode('submit'),



foreach($fields as $key=>$value) {




$ch = curl_init(); 

curl_setopt ($ch, CURLOPT_URL, $url); 

curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 

curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20070725 Firefox/"); 

curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 

curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0); 

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 

curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 

curl_setopt ($ch, CURLOPT_REFERER, $url); 

curl_setopt ($ch, CURLOPT_POST, count($fields)); 

curl_setopt ($ch,CURLOPT_POSTFIELDS, $fields_string);  

$result = curl_exec ($ch); 

echo $result;  



print_r('Curl error: ' . curl_error($ch));