1

我想登录 fashiondays.ro 但出错。这是我的代码我尝试了很多脚本但我似乎无法让它工作......

当前脚本:

<?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;
    }
}
?>
4

2 回答 2

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;                 
}
于 2013-03-07T07:55:22.000 回答
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);
}
于 2014-03-06T07:45:19.343 回答