2014-02-18 51 views
1

我想通過CURL從外部鏈接登錄到Joomla 3。從網上的一些調查結果後,我獲得此代碼一些調整:PHP + CURL + JOOMLA

<?php 
$uname = "admin"; 
$upswd = "admin"; 
$url = "http://localhost/joomla3/en/component/users"; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt'); 
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookie.txt'); 
curl_setopt($ch, CURLOPT_HEADER, FALSE); 

$results = curl_exec($ch); 

preg_match_all("(<input type=\"hidden\" name=\"return\" value=\"(.*)\" />)siU", $results, $matches1); 
preg_match_all("(<input type=\"hidden\" name=\"(.*)\" value=\"1\" />(.*)</fieldset>)iU", $results, $matches2); 

var_dump($matches1[1][0]); 
var_dump($matches2[1][0]); 

// POST fields 
$postfields = array(); 
$postfields['lang'] = 'en'; 
$postfields['option'] = 'com_users'; 
$postfields['view'] = 'login'; 
$postfields['task'] = 'user.login'; 
$postfields['username'] = urlencode($uname); 
$postfields['password'] = urlencode($upswd); 
$postfields['return'] = urlencode($matches1[1][0]); 
$postfields[urlencode($matches2[1][0])] = '1'; 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); 
$ret = curl_exec($ch); 
print_r($ret); 
?> 

我獲得的回報&關鍵,但無法登錄。哪裏不對? 謝謝。

+0

您獲得的輸出是什麼? – Raptor

+0

print_r($ ret);什麼都沒有返回(一個空字符串)。 –

+1

curl調試基礎:'curl_error()':http://www.php.net/manual/en/function.curl-error.php – Raptor

回答

2

我修改了代碼。 現在這項工作如預期,感謝您的支持。

<?php 
$uname = "admin"; 
$upswd = "admin"; 
$url_get_key = "http://localhost/joomla3/index.php?option=com_users&lang=en"; //MOD REWRITE Disabled 

//GET return & key 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url_get_key); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt'); 
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookie.txt'); 
curl_setopt($ch, CURLOPT_HEADER, 0); 

$results = curl_exec($ch); 

preg_match_all("(<input type=\"hidden\" name=\"return\" value=\"(.*)\" />)siU", $results, $matches1); 
preg_match_all("(<input type=\"hidden\" name=\"(.*)\" value=\"1\" />(.*)</fieldset>)iU", $results, $matches2); 

//var_dump($matches1[1][0]); 
//var_dump($matches2[1][0]); 

// POST 
$url_post = "http://localhost/joomla3/index.php?option=com_users&task=user.login&lang=en"; 
$postdata = "username=".urlencode($uname)."&password=".urlencode($upswd)."&return=".urlencode($matches1[1][0])."&".urlencode($matches2[1][0])."=1"; 
curl_setopt($ch, CURLOPT_URL, $url_post); 
curl_setopt($ch, CURLOPT_POST, TRUE); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); 
$results1 = curl_exec($ch); 

$url_data = "http://localhost/joomla3/index.php?option=com_users&lang=en"; //MOD REWRITE Disabled 
curl_setopt($ch, CURLOPT_URL, $url_data); 

$results2 = curl_exec($ch); 
$error = curl_error($ch); 
$errno = curl_errno($ch); 
echo ($error); 

curl_close($ch); 

//IF incorrect password 
if(@preg_match('#<div id="system-message">(.*)<p>(.*)</p>#siU', $results2, $matches3)){ 
    @preg_match('#<p>(.*)</p>#i', $matches3[0], $matches4); 
    echo $matches4[0]; 
} 

//IF Logged In 
if(@preg_match('#<div class="login-greeting">(.*)</div>#siU', $results2, $matches5)){ 
    echo $matches5[1]; 
} 

?>