2014-09-04 95 views
0

我對PHP很陌生,需要創建一個腳本來發送登錄信息,然後檢索結果。我做了一些周圍尋找,但,這是我能想出的最好:PHP腳本POST和檢索數據

<?php 
$postdata = http_build_query(
    array(
     'username' => 'username', 
     'password' => 'password' 
    ) 
); 

$opts = array('http' => 
    array(
     'method' => 'POST', 
     'header' => "Content-type: application/json", 
     'content' => $postdata 
    ) 
); 

$context = stream_context_create($opts); 
$url = "https://www.example.com/"; 
$fp = fopen($url, 'r', false, $context); 

$response = stream_get_contents($fp); 
echo json_encode($response); 
?> 

老實說,我不知道這是最好的辦法了什麼,我試圖做的,或者是有更好的方法。結果應該是一個令牌,我插入另一個POST,從數據庫中抽取特定用戶來填充基於用戶的主題。我也無法通過jQuery使用AJAX,因爲IE有問題(請參見圖)。任何幫助將不勝感激,我一直堅持這一段時間。

+0

那麼問題是什麼,爲什麼你沒有提到呢? – meda 2014-09-04 18:02:58

+0

我剛剛接觸PHP,不知道我有沒有工作。無論何時我拉動腳本並嘗試獲取數據,結果都會顯示「false」 – 2014-09-04 18:48:40

回答

2

您可以使用捲曲:

$url = "https://www.example.com/"; 

//open connection 
$ch = curl_init(); 

//set the url, POST data 
curl_setopt($ch,CURLOPT_URL, $url); 
curl_setopt($ch,CURLOPT_POST, TRUE); 
curl_setopt($ch,CURLOPT_POSTFIELDS, "username=username&password=password"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

//execute post 
$result = curl_exec($ch); 

//close connection 
curl_close($ch); 
+0

感謝您的幫助,我會給出一個答案,看看我能否得到我所需要的。 – 2014-09-04 18:46:29

0

您試圖登錄數據保存在超全局$_SESSION?這樣,只要你需要只是打電話給$_SESSION['UserLogin'];