PHP登录REST API

马克·托莱多

我正在尝试创建REST API并寻找使用PHP登录的方法,该文档提供了使用Python登录的示例,但是我不知道如何使用PHP进行登录。我在想下面是否有PHP版本的Python代码。

参见下面的代码:

def login():
global sessionID
req = urllib2.Request("https://<host>/appserver/j_spring_security_check")
req.add_data(urllib.urlencode({"j_username" : "admin","j_password" :"demoserver"}))
res = opener.open(req)
sessionID = getCookie("JSESSIONID",cookies)
# Get the value of JSESSIONID cookie
response = res.read()
return

如果需要使用PHP登录到Web服务(考虑Python示例),可以使用什么登录脚本(PHP版本)?

附加信息:

登录Web服务需要使用用户名和密码的JSON对象作为请求主体:

成功执行该方法将返回Cookie会话ID

请求JSON示例:{“ j_username”:“ username”,“ j_password”:“ *******”}

用户需要解析cookie并提取键为JSESSIONID的cookie。此JSESSIONID值需要手动添加到Rest调用的所有标头中

“ Cookie”:“ JSESSIONID =” + cookieValue

使用Python的另一个示例:

//Request for All Apps
global sessionID
sID = "JSESSIONID="+sessionID
uri = "https://<hostname>/appserver/portal/api/1.0/apps"
req = urllib2.Request(uri)
req.add_header("Content-Type", "application/json")
req.add_header("Cookie", sID) # Header
req.get_method = "lambda: GET” # Method Type
res = opener.open(req) # URL Call
response = res.read()
return response

请求标头:

Host: 192.168.100.100:444
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*\/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://192.168.100.100:444/appserver/portal/login;jsessionid=6AD37194D43AB02BB79E26C71554958F
Cookie: JSESSIONID=6AD37194D43AB02BB79E26C71554958F
Connection: keep-alive
Upgrade-Insecure-Requests: 1

----------

当我使用Linux尝试curl时,下面是代码。

curl -k -i -H "Content-type: application/x-www-form-urlencoded" -c cookies.txt -X POST https://192.168.100.100:444/appserver/j_spring_security_check -d "j_username=admin&j_password=demoserver"

这是linux curl的结果,我认为自从我被路由到欢迎页面以来,它已经成功连接了。

HTTP/1.1 302 Found
Date: Thu, 16 Feb 2017 18:41:59 GMT
Server: Apache/2.2.26 (Unix) mod_ssl/2.2.25 OpenSSL/1.0.1e mod_jk/1.2.37
Set-Cookie: JSESSIONID=358446CC1F87B2D698D48AFECA373691; Path=/appserver/; HttpOnly
Location: https://192.168.100.100:444/appserver/portal/welcome;jsessionid=358446CC1F87B2D698D48AFECA373691
Content-Length: 0
Access-Control-Allow-Origin: *
Content-Type: text/plain

----------

但是,当我尝试将PHP curl与代码结合使用时,仍然无法连接。

<?php
$ch = curl_init();
$url  = "https://192.168.100.100:444/appserver/j_spring_security_chec‌​k";
$postData = 'j_username=admin&j_password=demoserver';
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1); // -X
curl_setopt($ch, CURLOPT_POSTFIELDS,$postData); // -d
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'application/x-www-form-urlencoded'
)); // -H
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); // -c
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); // -c
curl_setopt($ch, CURLOPT_HEADER, true); // -i
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // -k
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo curl_exec ($ch);
curl_close ($ch);

这是浏览器中的结果标题。请求网址:http://localhost/curl.php请求方法:GET远程地址:127.0.0.1:80状态码:200 OK版本:HTTP

Response header:
Host: localhost
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/\*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
Upgrade-Insecure-Requests: 1

Response headers:
Date: Thu, 16 Feb 2017 18:43:52 GMT
Server: Apache/2.2.26 (Unix) mod_ssl/2.2.25 OpenSSL/1.0.1e mod_jk/1.2.37
Content-Language: en-US
Content-Length: 4815
Access-Control-Allow-Origin: *
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html;charset=UTF-8
Mevdschee

也许您需要cookie支持?像这样:

<?php
    // this is for cookie handling in the session
    session_start();
    $tmpFname = tempnam(sys_get_temp_dir(),"COOKIE");
    if (isset($_SESSION['cookies'])) {
        file_put_contents($tmpFname,$_SESSION['cookies']);
    }

    // the request
    $ch = curl_init();
    $url  = "https://<host>/appserver/j_spring_security_check";
    $postData = "j_username=admin&j_password=demoserver";
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$postData);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/x-www-form-urlencoded'
        // you may add more request headers here
    ));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // the next two options are for cookie handling
    curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpFname);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpFname);
    $server_output = curl_exec ($ch);
    echo $server_output;
    curl_close ($ch);

    // this is for cookie handling in the session
    $_SESSION['cookies'] = file_get_contents($tmpFname);
    unlink($tmpFname);

希望对您有所帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章