我对Laravel 5很陌生,但我正致力于涉及2台服务器(S1和S2)的项目。每个人都运行Laravel 5 REST WebService(API)WS1和WS2。
工作流程如下:
我使用Postman测试我的REST API。
使用Postman时,WS2运行良好。
但是当我尝试查询WS1时,我从来没有从WS2发送的响应中获取json。
希望我明白。
以下是我的一些源代码:
路由\ api.php
Route::get('/object/{obj_id?}', 'ObjectController@check')->where('obj_id', '[0-9]+');
应用\ HTTP \控制器\ ObjectController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ObjectController extends Controller
{
public function check($obj_id)
{
$token = 'my personnal access tokens from WS2 Laravel/Passport'
$client = new Client();
$body = $client->request('GET','http://S2.ip/api/object/' . $object_id, [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'bearer ' . $token,
])->getBody();
$contents = (string) $body;
$data = json_decode($contents);
dd($data); // to see what's inside $data
dd($ data)输出如下: null
我尝试过很多东西,但我从未经理过让json。 我究竟做错了什么。我真的很感激你的帮助。
由于
编辑:
以下是我从PostMan或Rested获得的S2答案:
使用Postman时,S2返回以下内容:
Response - http://s2.ip/api/object/1
200 OK
Headers
Date: Thu, 12 Jan 2017 08:38:31 GMT
Vary: Authorization
Server: Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.6.21
X-Powered-By: PHP/5.6.21
X-RateLimit-Remaining: 59
Content-Type: application/json
Cache-Control: no-cache
X-RateLimit-Limit: 60
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100
Content-Length: 46
Response body
{
"id": "1",
"authorized": false
}