I have the code below:
<?php
use Zend\Diactoros\Response;
$response = new Response('This is the response content');
echo $response->getBody()->getContents();
echo $response->getBody();
I'm passing the body directly in the constructor.
I'm trying to get the body of this response, nothing more then it, but when i call the getBody() or the getBody()->getContents() it's give me a empty string.
I've tried another alternative that works:
<?php
use Zend\Diactoros\Response;
$response = new Response;
$response->getBody()->write('This is the response content');
echo $response->getBody()->getContents();
echo $response->getBody();
and it outputs:
This is the response content This is the response content
Why the first and short form isn't working?
I found the problem, that's was my fault.
Actually, the Response __constructor gets a StreamInterface as first parameter, not a string.
The StreamInterface implementation is where you have to write your body, other wise, you get no response.
Here is the good approach:
$stream = new Stream('php://temp', 'rw');
$stream->write('This is a response');
$response = (new Response($stream));
echo $response->getBody();
Related
in vanilla php i would create a callbak url with just
try
{
//response content type application/json
header("Content-Type:application/json");
//read incoming request
$postData = file_get_contents('php://input');
.......
......
but in laravel i'm yet to get a clear explanation on how to achieve the same
ive tried using
$postData = Request::getContent();
but it returns blank
If you need data in request use (new \Illuminate\Http\Request())->all() or use DI
public function someAction(\Illuminate\Http\Request $request)
{
dd($request->all());
}
I'm writing a client for an API...
use Zend\Http\Client;
use Zend\Http\Request;
use Zend\Json\Json;
...
$request = new Request();
$request->getHeaders()->addHeaders([
'Accept-Charset' => 'UTF-8',
'Accept' => 'application/hal+json',
'Content-Type' => 'application/hal+json; charset=UTF-8',
]);
$apiAddress = 'http://my.project.tld/categories';
$request->setUri($apiAddress);
$request->setMethod('GET');
$client = new Client();
$response = $client->dispatch($request);
$data = $response->getContent();
... and get a broken JSON like this:
1f9e <-- What is it?
{"_links...
\u043 <-- What is it?
1a6...
tfoli <-- What is it?
0
The string is separaten into five lines:
1st line: only 1f9e
2nd line: first content part
3d line: string 1a6
4th line: the second content part
5th line: 0
Why am I getting additional symbols/strings? How to avoid this a get valid JSON output?
The problem with the getContent() method of the response object. It may not decode the content it gets in it from the request. Please have a look at here. This might be the reason. I may be wrong!
So the getBody() method of it does the decoding job for the content of the request. So please use this method instead of getContent().
$data = $response->getBody();
Hope this would help you!
I'm using Guzzle to get an HTTP response. If I do this:
$response = $res->getBody();
I get an object with 'email' as one of the properties. But if I do either:
$email = $res->getBody()->email;
or
$email = $response->email
I get a 'No value for email' error. What am I missing?? How can I access a specific property in the response object?
The getBody method returns an instance of StreamInterface. You first need to retrieve the contents of the response:
$response = (string) $res->getBody();
Only then you can decode the json payload:
$json = json_decode($response);
$email = $json->email;
I'm tyring to post something to Googles oAuth server to do some authentication. Google really wants this information in form data (Content-Type: application/x-www-form-urlencoded) but my guzzle client seems to insist (as far as I can tell) on making the body JSON. I'm using Guzzle 4.*
I've changed my URL to a PostCatcher.io url, so I can see what comes out (because for the life of my I can't figure out how to see the actual raw HTTP request that guzzle spits out), and it looks like theres JSON coming out.
My code (I'm using a test url by now):
$client = new GuzzleClient();
$url = "https://www.googleapis.com/" . "oauth2/v3/token";
$test_url = 'http://postcatcher.in/catchers/55602457b92ce203000032ae';
$request = $client->createRequest('POST', $test_url);
$request->setHeader('Content-Type', 'application/x-www-form-urlencoded'); //should be redundant
$body = $request->getBody();
$body->setField('code', $code);
$body->setField('client_id', $this->client_id);
$body->setField('client_secret', $this->client_secret);
$body->setField('redirect_url', $this->redicrect_url);
$body->setField('grant_type', $this->grant_type);
try {
$response = $client->send($request);
$result = $response->getBody();
return $result;
} catch (\Exception $exc) {
return $exc->getCode() . ' ' . $exc->getMessage();
}
The documentation says this should be enough. What am I missing ?
Using Guzzle 5.2 I've done the same thing with:
$request = $this->createRequest(
$method,
$uri,
['body' => $php_array_of_values ]
);
$response = $this->send($request);
I found the problem. I did upgrade to Guzzle 5.* tho I suspect that wasn't actually the solution.
I just neede to look at the response content. So in the exception clause of the call I added:
if ($exc->hasResponse()) {
return $exc->getResponse()->json();
}
Which gave me a clear error message from Google. The error message was "Missing parameter: redirect_uri". And that's because I had written url instead of uri.
Fixed that, and now it's all good.
I asked a similar question earlier, in a nutshell I have an API application that takes json requests and outputs an json response.
For instance here is one of the requests that I need to test out, how can I use this json object with my testing to emulate a 'real request'
{
"request" : {
"model" : {
"code" : "PR92DK1Z"
}
}
The response is straightforward (this bit has been done).
From other users on here this is the optimised method using Yii to do this, I am just unsure how to emulate the json request - e.g essentially send a JSON HTTP request, can anyone assist on how to do this?
public function actionMyRequest() {
// somehow add my json request...
$requestBody = Yii::app()->request->getRawBody();
$parsedRequest = CJSON::decode($requestBody);
$code = $parsedRequest["request"]["model"]["code"];
}
I don't understand if you want your app to send an http request and get the result or at the opposite receive a http request
I answered for the first assumption, I'll change my answer if you want the other
For me the best way to send an HTTP request is to use Guzzle http client.
This is not a yii extension, but you can use third party libraries with yii.
Here's an example from Guzzle page:
$client = new GuzzleHttp\Client();
$res = $client->get('https://api.github.com/user', [
'auth' => ['user', 'pass']
]);
echo $res->getStatusCode(); // 200
echo $res->getHeader('content-type'); // 'application/json; charset=utf8'
echo $res->getBody();
So in your case you could do something like:
public function actionMyRequest() {
$client = new GuzzleHttp\Client();
$res = $client->get('https://api.your-url.com/');
$requestBody = $res->getBody();
$parsedRequest = CJSON::decode($requestBody);
$code = $parsedRequest["request"]["model"]["code"];
}