[英]php process http authentication
我有一個服務器,在提供個性化的json結果之前提示進行http身份驗證。
我如何編寫一個在另一個框上運行的PHP腳本來提示auth,傳遞它並拉出結果?
只需創建一個包含登錄名和密碼輸入的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的東西。
確保使用SSL。 否則,任何人都可能劫持您未加密的憑證。
更改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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.