2012-01-09 75 views
0

我有一個服務器,它會提供個性化的json結果之前提示進行http身份驗證。php進程http身份驗證

我該如何編寫一個在另一個框上運行的php腳本來提示auth,將它傳遞並提取結果?

+0

可以捲曲 – 2012-01-09 19:50:05

回答

1

只需創建一個帶登錄名和密碼輸入的HTML表單,然後用cURL檢索數據。

$curl = curl_init('http://example.com/api'); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_USERPWD, $_POST['login'].':'.$_POST['password']); 
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 

$response = curl_exec($curl); 

如果你想有更多的「互動」嘗試添加一些Ajax的東西。

0

更改USER:PASS爲用戶名和密碼,並將URL更改爲您的URL。返回值在$ jsonStr中。

// create a new cURL resource 
$ch = curl_init(); 

// set URL and other appropriate options 
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); 

// Puts return in variable rather than the browser 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt($ch, CURLOPT_USERPWD, "USER:PASS"); 

// grab URL and pass it to the variable 
$jsonStr = curl_exec($ch); 

// close cURL resource, and free up system resources 
curl_close($ch); 
1

確保這是通過SSL進行的。否則,任何人都可能劫持你的未加密的證書。