0

I am sending a request from the client using GuzzleHttp\Client to a page on another port that is serving as a server like

use SL\Controllers\LoginController;
use SL\FileHelper;
use GuzzleHttp\Client;

require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . "config/config.php";

$client = new Client();
$response = $client->request('get','http://myip:75/src/LoginVerification.php?username=asd&password=asd');

The request is served on the page

require_once(__DIR__ . '/../config.php');

use Moodle\Manager\UserManager;
use GuzzleHttp\Psr7\Response;

$userManager = new UserManager();
$user;


$username = $_GET["username"];
$password = $_GET["password"];

$isLogin = $userManager->verifyLogin($username, $password, $user);
if($isLogin)
    $body = "yahoo";
else
    $body = "oooops";

$status = 201;
$headers = ['Content-Type' => 'application/json'];
$protocol = '1.1';
$response = new Response($status, $headers, $body, $protocol);
return $response;

please ignore the password passing in GET as my main focus here is to send requests and get responses.

After the request is served on the server and response that I made is as below

phrases:array(58)
reasonPhrase:"Created"
statusCode:201
headers:array(1)
headerNames:array(1)
protocol:"1.1.1"
stream:GuzzleHttp\Psr7\Stream
stream:resource id='10' type='stream'
size:null
seekable:true
readable:true
writable:true
uri:"php://temp"

But what is received is this

headerNames:array(9)
headers:array(9)
statusCode:200
reasonPhrase:"OK"
phrases:array(58)
protocol:"1.1"
headerNames:array(9)
headers:array(9)
phrases:array(58)
reasonPhrase:"OK"
statusCode:200
stream:resource id='4' type='stream'
size:null
seekable:true
readable:true
writable:true
uri:"php://temp"

It is not the same response that I had created.

yousaf
  • 1
  • 4
  • What's the difference between the two that you are struggling with? What have you tried to check **why** there are differences? – Nico Haase Jun 23 '21 at 10:10
  • Nico, the First that I have mentioned is a response created by me and it includes that in the stream when i get it right after setting in body [$response->getBody()->getcontents()] to verify that data is saved in response. but the second response is independent to what I do on the server side, if I don't create a response on server even then same reponse will be returned – yousaf Jun 23 '21 at 10:20
  • `return $response` does not look like you are really **sending** the response – Nico Haase Jun 23 '21 at 10:39
  • Does https://stackoverflow.com/questions/43332737/using-guzzlehttp-psr7-response-correctly help? – Nico Haase Jun 23 '21 at 10:45
  • I had tried this as well but in this case, the user only sending status.. but I want to send the data in the body as well – yousaf Jun 23 '21 at 10:54
  • And how do you properly send the response then? Using an object of a package that is meant to be used client-side does not work the same way as sending all the headers and properly handling the body – Nico Haase Jun 23 '21 at 11:24
  • Nico, actually m not sing Laravel so I think sending a response of type GuzzleHttp\Psr7\Response might not work, so I did an alternative thing, you can see it in my answer section. – yousaf Jun 24 '21 at 06:57
  • Laravel does not know anything about that response object. The linked answer at https://stackoverflow.com/questions/43332737/using-guzzlehttp-psr7-response-correctly provides a good starting point for generating a **proper** HTTP response from that response object. Have you tried to use it? – Nico Haase Jun 24 '21 at 08:29

1 Answers1

0

As I m not using Laravel so this issue of not getting the response that I want can be solved by making a simple stdClass object and echo this object by doing json_encode and then you can get the data in the response body.

$response = new stdClass();
$isLogin = $userManager->verifyLogin($username, $password, $user);
    if($isLogin)
        $response->body = "yahoo";
    else
        $response->body = "oooops";
    
    $response->status = 201;
    $response->headers = ['Content-Type' => 'application/json'];
    $response->protocol = '1.1';
    echo json_encode($response);

To get it on the client-side you just have to do this

$data = $response->getBody()->gtContents();
yousaf
  • 1
  • 4