2013-03-07 292 views
1

登錄我要登錄fashiondays.ro但error.This是我的代碼 我試過很多劇本,但我似乎無法得到它的工作...PHP捲曲不能捲曲

當前腳本:

<?php 
$loginUrl = 'https://www.fashiondays.ro/login_check/'; 
$email = 'thomanphan%40gmail.com';//demo user name 
$password = '1234567890';//demo pass 


$cookie_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . "cookies.txt"; 
if (!file_exists($cookie_file)) 
{ 
    $ourFileHandle = fopen($cookie_file, 'w') or die("can't open file"); 
    fclose($ourFileHandle); 
} 
// make list of POST fields 
$fields = array(
    '_username' => urlencode($email), 
    '_password' => urlencode($password), 
    '_country_code' => urlencode('ro'), 
    '_remember_me' => urlencode('1') 
); 
$fields_string = ''; 
foreach ($fields as $key => $value) { 
    $fields_string .= $key . '=' . $value . '&'; 
} 
rtrim($fields_string, '&'); 
$ch = curl_init(); 
$headers = array(
    'accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 
    "content-length: " . count($fields_string) //instead of 0, how could I get the 
    length of the body from curl? 
); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //set headers 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_URL, $loginUrl); 
curl_setopt($ch, CURLOPT_POST, count($fields)); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); 
curl_setopt($ch, CURLOPT_REFERER, 'https://www.fashiondays.ro/login/'); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
$result = curl_exec($ch); 
if ($result === false) { 
    die('CURL ERROR: ' . curl_error($ch)); 
} else { 
    curl_setopt($ch, CURLOPT_URL, 'http://www.fashiondays.ro/campaigns/'); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $result = curl_exec($ch); 
    curl_close($ch); 
    if ($result === false) { 
     die('CURL ERROR: ' . curl_error($ch)); 
    } else { 
     echo $result; 
    } 
} 
?> 
+0

什麼錯誤? – 2013-03-07 05:13:01

+0

網站返回http://www.fashiondays.ro/login/並且無法登錄 – Thoman 2013-03-07 05:14:08

+0

請勿對您的電子郵件地址進行urlencode,它將用%40替換@。 – shapeshifter 2013-03-07 05:36:10

回答

1

試試這個簡單的功能來做登錄。

發送參數數組CURLOPT_POSTFIELDS

/** 
* If login is required use this function 
* to have session/cookie. 
*/ 
function logIn($loginActionUrl,$parameters) 
{ 
     curl_setopt ($this->curl, CURLOPT_URL,$loginActionUrl); 
     curl_setopt ($this->curl, CURLOPT_POST, 1); 
     curl_setopt ($this->curl, CURLOPT_POSTFIELDS, $parameters); 
     curl_setopt ($this->curl, CURLOPT_COOKIEJAR, realpath('cookie.txt')); // cookie.txt should be in same directoy, where calling script is 
     curl_setopt ($this->curl, CURLOPT_COOKIEFILE, realpath('cookie.txt')); 
     curl_setopt ($this->curl, CURLOPT_FOLLOWLOCATION, 1); 
     curl_setopt ($this->curl, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt ($this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i586; de; rv:5.0) Gecko/20100101 Firefox/5.0');    
     curl_setopt ($this->curl, CURLOPT_REFERER, 'http://www.google.com'); // set referer 
     curl_setopt ($this->curl, CURLOPT_SSL_VERIFYPEER, FALSE);// ssl certificate 
     curl_setopt ($this->curl, CURLOPT_SSL_VERIFYHOST, 2); 

     $result['EXE'] = curl_exec($this->curl); 
     $result['INF'] = curl_getinfo($this->curl); 
     $result['ERR'] = curl_error($this->curl); 
     return $result;     
} 
+0

你可以編寫完整的腳本。 – Thoman 2013-03-07 08:01:11

0

在ImpressPages我已經做到了這種方式:

//initial request with login data 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/login.php'); 
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36'); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=XXXXX&password=XXXXX"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_COOKIESESSION, true); 
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name'); //could be empty, but cause problems on some hosts 
curl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/ip4.x/file/tmp'); //could be empty, but cause problems on some hosts 
$answer = curl_exec($ch); 
if (curl_error($ch)) { 
    echo curl_error($ch); 
} 

//another request preserving the session 

curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/profile'); 
curl_setopt($ch, CURLOPT_POST, false); 
curl_setopt($ch, CURLOPT_POSTFIELDS, ""); 
$answer = curl_exec($ch); 
if (curl_error($ch)) { 
    echo curl_error($ch); 
}