2012-01-09 79 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进行的。否则,任何人都可能劫持你的未加密的证书。