1

I'm working on a project that need to call a API to get json response, but the Guzzle client auth is not working. It always redirect to the login page. "nasa" is the database. Here's my code:

<?php
require_once '../composer/vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client([
    'base_url' => ['http://fedview.bfountain.com/{datasource}/', ['datasource' => 'nasa']]]);
$response = $client->get('service/sbapp/goalByAgency', ['auth' =>  ['username', 'password']]);
echo $response->getBody();
?>

I tried:

$response = $client->get('login.do?uname=username&passcode=password');

It can log in successfully.

Can anyone help me on this? (I'm using Guzzle 5) THX!

Layla Wang
  • 65
  • 7
  • When giving the auth credentials in the get method try without the 'auth' key. exa: ```$response = $client->get('service/sbapp/goalByAgency', ['username', 'password']);``` – arikin May 08 '15 at 08:06
  • Ohhh. Wait. Using the login.do url works? – arikin May 08 '15 at 08:36
  • That is NOT basic auth. Do a get and send with the ```login.do?uname=username&passcode=password``` first then get and send the ```service/sbapp/goalByAgency``` – arikin May 08 '15 at 08:53

1 Answers1

0

Here is the WHY - logic of what happens I will have to leave the rest of troubleshooting for you.

1) echo $response->getBody(); // prints out the login page "It always redirect to the login page"

2) $response = $client->get('service/sbapp/goalByAgency', ['auth' => ['username', 'password']]); // your $response is a login page because you ask $client->get the login page or authenticate page

3) You need to authenticate successfully for a $client 4) Then use the $client to request data via POST method (you missed this step)

see my other post: How can I use Guzzle to send a POST request in JSON?

redo your code. you are on the right track.

Community
  • 1
  • 1
Dung
  • 19,199
  • 9
  • 59
  • 54