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 に答える