My Problem is relatively simple:
I make an API call and get the following answer in response:
[{"RowKey":24764}]
The Content-Type I'm receiving is text/html
Somehow, I'm just not able to parse this correctly. neither json_encodeor json_decodeseems to help.
I'm trying to Map the object into my response class:
class ApiResponse {
public $schedules = [];
}
with the json object mapper from: https://github.com/mintware-de/json-object-mapper
Consider this test case:
$response = '[{"RowKey":24764}]';
$result = json_decode($response, true);
echo $result[0]['RowKey']; // Output: 24764
Related
I've edited this question as I realised I was completely on the wrong track, however I still have an issue.
Using Guzzle, how do I send an object in JSON-form from my shop server, which does not use Laravel, to my returns server, which does use Laravel?
I keep receiving the following error:
Client error: `POST https://returns.jdoe.blah.test/createReturn` resulted in a `419 unknown status`.
I think it has something to do with the fact that I don't have a token, but I don't know what to do with it. I know that Laravel uses CSRF tokens, but my shop server does not use that form.
In the shop server, when a user makes an order, it is saved in the object "$order". I added the following code to order_details.php, in an attempt to pass two particular attributes of the order object:
$client = new Client();
$url = "https://returns.jdoe.blah.test/createReturn";
$post_data = array(
'orderId' => $order['aufnr'],
'customerId' => $order['kundennummer']
);
$data = json_encode($post_data);
$request = $client->post($url, array(
'content-type' => 'application/json'
));
$request->setBody($data);
$response = $request->send();
Then in my Laravel project, I have:
web.php
Route::post('/createReturn', 'ProductReturnsController#createReturn');
ProductReturnsController.php
<?php
namespace App\Http\Controllers;
use App\ProductReturn;
use Illuminate\Http\Request;
class ProductReturnsController extends Controller
{
public function createReturn($json)
{
echo "hallo";
/* $jsonDecoded = json_decode($json);
$orderId = $jsonDecoded['orderId'];
echo $orderId;*/
return view('data');
}
}
data.blade.php
<html>
<head>
Test
</head>
<body>
This is a test page.
</body>
</html>
If you need anything else from me to help me solve this, please don't hesitate to ask. Thanks :).
The response to your question is actually on the first page of Guzzle documentation: http://docs.guzzlephp.org/en/stable/
You are doing var_dump($response) which is in fact the response object for the request you made. That object has a method getBody(),
So try doing
dd($response->getBody());
instead.
Try dd($response->getBody()->getContents()) or dd((string) $response->getBody()). Response body is a stream object, so if you want a string you have to do an additional method call.
I have this response content from api response:
{"token_type":"Bearer","expires_in":31535919,"access_token":"eyJ0eXa99e760ef4e468d17198f9fa6be9d67c36240a9bf9bb74d76b18e18ca2661706e33d1bc80cdAiOiJKV1QiLCJhbGciOiJSU.......
How I can get the access_token value? So I can use it.
You can use json_decode function to conver that into object and access it there.
$response = '{"token_type":"Bearer","expires_in":31535919,"access_token":"eyJ0eXa99e760ef4e468d17198f9fa6be9d67c36240a9bf9bb74d76b18e18ca2661706e33d1bc80cdAiOiJKV1QiLCJhbGciOiJSU.......';
$response = json_decode($response, true);
echo $response['access_token'];
Hope this helps.
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());
}
Im calling a vehicle data API at an endpoint and can return the data fine using:
$client = new Client();
$url = "https://uk1.ukvehicledata.co.uk/api/MISC DETAILS;
$result = $client->get($url);
return $result->request;
This is using mock data so the response is:
{
"Request":{
"RequestGuid":"",
"PackageId":""
"Response":{
// VEHICLE DATA
}
}
However, I now wish to store the response in the database but cannot access ->request or ->Request etc.
Using:
return json_encode( (array)$result );
Actually returns the headers from Guzzle and no response data.
Any help?
You have to convert the body into JSON first:
$jsonArray = json_decode($result->getBody()->getContents(), true);
echo $jsonArray['Request']['RequestGuid'];
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"];
}