Pear ::从命令行访问Auth

时间:2013-03-14 17:52:38

标签: php http pear

我对Pear :: Auth PHP库有疑问。我可以以某种方式“登录命令行”。我的意思是我可以从HttpRequest访问受Pear :: Auth保护的资源我在其他脚本程序中创建自己吗?你能给我一个例子(python,php,java或任何可读的东西)

3 个答案:

答案 0 :(得分:0)

真的取决于...你想要做什么以及从哪里做到这一点......有些事情会浮现在脑海中:

  • 将凭据作为命令行参数传递。
  • 将凭据作为GET请求参数的一部分传递(可能假设为SSL)。
  • 将cURL与饼干罐一起使用。
  • 实现服务层,以便这些调用以另一种方式处理身份验证,以满足您的需求(具有与您现有Pear身份验证协同工作的身份验证的API)。

答案 1 :(得分:0)

好的,让它工作(希望这可能会帮助其他人 - 这是可以转换为任何语言的Python解决方案,并允许您通过在程序中创建HttpRequest控件来登录Pear :: Auth。

import urllib
import httplib2
from urllib import urlencode
http = httplib2.Http()

url = 'LOGIN_URL'
# this applies to current version of Pear (had to add the authsecret)
body = {'username': 'USRENAME', 'password': 'PASSWORD', 'authsecret': ''} 
headers = {'Content-type': 'application/x-www-form-urlencoded'}
response, content = http.request(url, 'POST', headers=headers, body=urllib.urlencode(body))
# CAREFUL!!! HERE IS THE TRICKY PART !
# response['set-cookie'] looks like this -> 'SomeSession=longID; path=/, SomeSession=longID; path=/, authchallenge=blabla; path=/'
# you need to parse it and use only authchallanage and SomeSessionpart(just one of them I think the last one) - have no idea why, but it works. So next line needs some parsing and fixing
headers = {'Cookie': response['set-cookie']} #parseme -- this is not correnct and will not work, but you have to fix it to match your implementation
print headers

# e.g. headers = {'Cookie': 'MySession=bdfdstiq90oilkpk7n4s2q2g50; authchallenge=fddtggffg5784d359c12dfad4059', 'X_REQUESTED_WITH': 'xmlhttprequest'}#the second header is optional, I needed to access some ajax call
data = dict(argument="to_pass", eg="customerID")
resp, content = http.request("secure_URL", "POST", urlencode(data), headers=headers)
print resp
print content

答案 2 :(得分:0)

如果有人对Java解决方案感兴趣,那就是:

public String getServerJson(){
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httpost = new HttpPost("https://"+hostToReach+"/secure/index.php");
    httpost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");

    List <NameValuePair> nvps = new ArrayList <NameValuePair>();
    nvps.add(new BasicNameValuePair("username", htmlUsername));
    nvps.add(new BasicNameValuePair("password", htmlPassword));

    httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
    try {
        HttpResponse response = httpclient.execute(httpost);
        HttpEntity entity = response.getEntity();
        EntityUtils.consume(entity);

    }
    catch (Exception e){
        e.printStackTrace();
    }

    HttpPost ajaxPost = new HttpPost("https://"+hostToReach+"/?event_id="+pmp.getPmpEventId().getEventId().toString()+"&categories_only=true&pos=true");
    ajaxPost.setHeader("X_REQUESTED_WITH", "xmlhttprequest");
    try {
        HttpResponse catResponse = httpclient.execute(ajaxPost);
        BufferedReader rd = new BufferedReader (new InputStreamReader(catResponse.getEntity().getContent()));
        String line = "";
        String json = "";
        while ((line = rd.readLine()) != null) {
              json += line;
        }
        EntityUtils.consume(catResponse.getEntity());
        return json;


    }
    catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}

注意:Cookie会自动处理(httpclient会在其存在的整个时间内保留它们,因此您无需担心)