使用PHP cURL通过REST API获取JSON [英] Fetch JSON via REST API using PHP cURL

查看:419
本文介绍了使用PHP cURL通过REST API获取JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PHP cURL通过REST API检索JSON数据。



在这篇文章中使用PHP代码,我可以绕过Spring Security页面网址:

  https://< host> / appserver / j_spring_security_check 

但是,此URL中存在JSON,因此我要获取它:

  https://< host> /appserver/portal/api/1.0/apps 

这是我用来自动登录的PHP代码,我尝试使用 file_get_contents(url)它失败,只有以下代码工作确认登录。

 <?php 
function login(){
$ headers [] =Accept:* / *;
$ headers [] =连接:Keep-Alive;
$ headers [] =Content-type:application / x-www-form-urlencoded; charset = UTF-8;
$ data =j_username = admin& j_password = demoserver;
$ data = rtrim($ data,&);
global $ sessionid;
$ sessionid = dirname(__ FILE __)。'/ cookie2.txt'
$ ch = curl_init();
$ options = array(
CURLOPT_REFERER =>https://< host> / appserver / portal / login,
CURLOPT_HTTPHEADER => $ headers,
CURLOPT_COOKIEFILE => $ sessionid,
CURLOPT_COOKIEJAR => $ sessionid,
CURLOPT_URL ="https://< host> / appserver / j_spring_security_check,
CURLOPT_SSL_VERIFYHOST =
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $ data,
CURLOPT_HEADER => true,
CURLOPT_USERAGENT => Mozilla / 5.0(X11; Ubuntu; Linux x86_64; rv:51.0)Gecko / 20100101 Firefox / 51.0');
curl_setopt_array($ ch,$ options);
$ content = curl_exec($ ch);
curl_close($ ch);
}
login();
$ text = file_get_contents($ sessionid);
preg_match('/ \w {32} / u',$ text,$ match);
$ jsession = implode($ match);

$ headers1 [] =Accept:* / *;
$ headers1 [] =连接:Keep-Alive;
$ headers1 [] =Content-type:application / json;
$ headers1 [] =Cookie:JSESSIONID =。$ jsession;
$ url ='https://< host> /appserver/portal/api/1.0/apps';
$ ch2 = curl_init();
curl_setopt($ ch2,CURLOPT_URL,$ url);
curl_setopt($ ch2,CURLOPT_HTTPHEADER,$ headers1);
curl_setopt($ ch2,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch2,CURLOPT_COOKIEFILE,$ sessionid);
curl_setopt($ ch2,CURLOPT_COOKIEJAR,$ sessionid);
curl_setopt($ ch2,CURLOPT_HTTPGET,1);
$ result = curl_exec($ ch2);
curl_close($ ch2);
// var_dump(json_decode($ result,true));
$ output = json_decode($ result);

这是执行PHP代码后的结果:


$ b $ HTTP / 1.1 302找到的日期:Fri,17 Feb 2017 03:59:00 GMT
服务器:Apache / 2.2.26(可用的版本) Unix)mod_ssl / 2.2.25 OpenSSL / 1.0.1e mod_jk / 1.2.37
Set-Cookie:JSESSIONID = DB7139F7B8EE30F868A8392A4BF15523; Path = / appserver /; HttpOnly
位置:https://192.168.100.100:444/appserver/portal/welcome
Content-Length:0
访问控制允许原产地:*
Keep- Alive:timeout = 5,max = 100
Connection:Keep-Alive
Content-Type:text / plain

$ b b

如何根据上述cURL选项获取JSON?

解决方案

当你已经使用cookiefile和cookiejar手动cookie头?这不应该需要。



也可能是您收到一个setcookie标头,可能是服务器不接受您正在发送的会话cookie。



此外,为什么第二个请求不忽略无效的SSL?



最后,为什么不在第二个请求上发送相同的用户代理?



如果您修复这些问题,您将看到预期的行为。


I want to retrieve a JSON data via REST API using PHP cURL.

Using the PHP code in this post, I was able to bypass the Spring Security page with the URL:

https://<host>/appserver/j_spring_security_check

However, the JSON exist inside this URL and I want to fetch it:

https://<host>/appserver/portal/api/1.0/apps

This is the PHP code I used to login automatically, I tried using file_get_contents(url) but it failed, only the below code worked to confirm login.

<?php
function login(){
    $headers[]      = "Accept: */*";
    $headers[]      = "Connection: Keep-Alive";
    $headers[]      = "Content-type: application/x-www-form-urlencoded;charset=UTF-8";
    $data           = "j_username=admin&j_password=demoserver";
    $data           = rtrim($data, "&");
    global $sessionid;
    $sessionid      = dirname(__FILE__).'/cookie2.txt'
    $ch                 = curl_init(); 
    $options = array(
        CURLOPT_REFERER             => "https://<host>/appserver/portal/login",
        CURLOPT_HTTPHEADER          => $headers,
        CURLOPT_COOKIEFILE          => $sessionid,
        CURLOPT_COOKIEJAR           => $sessionid,
        CURLOPT_URL                 => "https://<host>/appserver/j_spring_security_check",
        CURLOPT_SSL_VERIFYHOST      => 0,
        CURLOPT_SSL_VERIFYPEER      => false,
        CURLOPT_POST                => true,
        CURLOPT_POSTFIELDS          => $data,
        CURLOPT_HEADER              => true,
        CURLOPT_USERAGENT           => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:51.0) Gecko/20100101 Firefox/51.0');
    curl_setopt_array($ch, $options);
    $content            = curl_exec($ch);
    curl_close($ch);
}
login();
$text = file_get_contents($sessionid);
preg_match('/\w{32}/u', $text, $match);
$jsession = implode($match);

    $headers1[]         = "Accept: */*";
    $headers1[]         = "Connection: Keep-Alive";
    $headers1[]         = "Content-type: application/json";
    $headers1[]         = "Cookie: JSESSIONID=".$jsession;
$url = 'https://<host>/appserver/portal/api/1.0/apps';
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, $url);
curl_setopt($ch2, CURLOPT_HTTPHEADER, $headers1);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_COOKIEFILE, $sessionid);
curl_setopt($ch2, CURLOPT_COOKIEJAR, $sessionid);
curl_setopt($ch2, CURLOPT_HTTPGET, 1);
$result = curl_exec($ch2);
curl_close($ch2);
// var_dump(json_decode($result, true));
$output = json_decode ( $result );

This is the result after execution of PHP code:

HTTP/1.1 302 Found Date: Fri, 17 Feb 2017 03:59:00 GMT
Server: Apache/2.2.26 (Unix) mod_ssl/2.2.25 OpenSSL/1.0.1e mod_jk/1.2.37
Set-Cookie: JSESSIONID=DB7139F7B8EE30F868A8392A4BF15523; Path=/appserver/; HttpOnly
Location: https://192.168.100.100:444/appserver/portal/welcome 
Content-Length: 0
Access-Control-Allow-Origin: *
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/plain  

How can I fetch the JSON given the above cURL options?

解决方案

Why are you setting the cookie header manually when you already use cookiefile and cookiejar? That should not be needed.

It is also suspicious that you receive a setcookie header, probably the server is not accepting the session cookie you are sending.

Also, why are you not ignoring invalid SSL on the second request?

Finally, why are you not sending the same user-agent on the second request?

I guess that if you fix these problems you will see the expected behavior.

这篇关于使用PHP cURL通过REST API获取JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆