douwen5546
2018-01-08 17:31
浏览 115
已采纳

将API与laravel项目连接

I need to connect to an API with my Laravel 5.1 project so I write function to test:

public function testDng() {

    $client = new GuzzleHttp\Client(['verify' => false ]);
try {
    $res = $client->post('https://api.example.com/api/authenticate', [

    'form_params' => [
        'email' => 'john@example.com',
        'password' => 'asdasd'
]
            ]);
return $res;
}
catch (GuzzleHttp\Exception\ClientException $e) {
        $response = $e->getResponse();
        $result =  json_decode($response->getBody()->getContents());

    return response()->json(['data' => $result]);

    }

}

The result of this function is:

{"data":null}

but when I try this API in Postman app everything is fine and I get token:

enter image description here

So something is wrong with my function ... What caused the problem and why I cant get token?

图片转代码服务由CSDN问答提供 功能建议

我需要使用Laravel 5.1项目连接API,以便编写测试函数: \ n

  public function testDng(){
 
 $ client = new GuzzleHttp \ Client(['verify'=> false]); 
try {
 $ res = $ client-&gt  ; post('https://api.example.com/api/authenticate',[
 
'form_params'=> [
'电子邮件'=>'john@example.com',
'  password'=>'asdasd'
] 
]); 
return $ res; 
} 
catch(GuzzleHttp \ Exception \ ClientException $ e){
 $ response = $ e-> getResponse();  
 $ result = json_decode($ response-> getBody() - > getContents()); 
 
返回response() - > json(['data'=> $ result]); 
  
} 
 
} 
   
 
 

此函数的结果是:

  {“data”  :null} 
   
 
 

但是当我在Postman应用程序中尝试这个API时,一切都很好,我得到了令牌:

因此我的功能出现了问题......导致问题的原因及原因 我不能获得令牌?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doyrte8419 2018-01-08 19:12
    已采纳

    In your error it says https://api.dineandgift.com/api/authenticate but in the Postman screenshots the URL (in the hidden tabs) starts with https://app.di[...]

    打赏 评论

相关推荐 更多相似问题