i get a json response like this note i am using GuzzleHttp to call my API
$response = $response->getBody()->getContents();
$output = (json_decode($response));
dd(output)
{#232 ▼
+"current_page": 1
+"data": array:2 [▼
0 => {#230 ▼
+"id": 1
+"test_col": "Test one"
}
1 => {#237 ▼
+"id": 3
+"test_col": "Test three"
}
]
+"first_page_url": "http://api/api/test?page=1"
+"from": 1
+"last_page": 8
+"last_page_url": "http://api/api/test?page=8"
+"next_page_url": "http://api/api/test?page=2"
+"path": "http://api/api/test"
+"per_page": 2
+"prev_page_url": null
+"to": 2
+"total": 15
}
and now when i go to my front end and do this
{{$outputs->links()}}
to get the pagination links it shows me an error
ErrorException (E_ERROR) Call to undefined method stdClass::links()
and on my API side i am doing this
$results = DB::table('test_table')->paginate(2);
return ($results);
The object from the response ($output) can not have any methods. The response already has links (first_page_url, last_page_url, next_page_url). If you need to HTML rendered links then you can try to create instance of LengthAwarePaginator
$pagination = new LengthAwarePaginator($outputs->data, $outputs->total, $outputs->per_page, $outputs->current_page);
echo $pagination->links();
(not tested)
Related
I use GuzzleHttp to send data via "_bulk" to an Elastic Search index. It is only a small dataset of 850 records. When I transfer the data record by record, I get an error message for 17 records. That's fine for me, so I can fix the errors.
But when I use _bulk, I do not get any error message at all. The 17 incorrect records are just ignored and are missing inside the index. How can I get an error message here? Are there some kind of options that I can use? Any ideas?
The endpoint is:
Here are my main code parts:
$jsonData = "xxxxx"; // the payload for the request
$elasticUrl = "https://xxxx.xx/xxxxx/_doc/_bulk";
$client = new Client([
"verify" => false, // disable ssl certificate verification
"timeout" => 600, // maximum timeout for requests
"http_errors" => false // disable exceptions
]);
$header = ["Content-Type" => "application/json"];
$result = $client->post($elasticUrl,
[
"headers" => $header,
"body" => $jsonData
]
);
if ($result->getStatusCode() != 200) {
$ret = "Error ".$result->getStatusCode()." with message: ".$result->getReasonPhrase();
}
A bulk request will always succeed with HTTP 200.
However, in the bulk response, you should see an indication whether each item succeeded or not. If you see errors: true in the response, then you know some of the items could not get indexed and looking into the items array, you'll find the error for the corresponding items.
As #Val pointed out the use of $response->getBody() gives the needed information:
$body = (string) $result->getBody();
$bodyArray = json_decode($body, true);
if ($bodyArray["errors"]) {
$retArray = [];
foreach ($bodyArray["items"] as $key => $item) {
if (isset($item["create"]["error"])) {
$retArray[] = $item["create"]["error"]["reason"].": ".json_encode($data[$key]);
}
}
$ret = implode(", ", $retArray);
}
As side note: in $data I keep the data as php array before sending it to Elastic Search.
I'm using the following endpoint to add an attachment to an envelope:
PUT /v2.1/accounts/{accountId}/envelopes/{envelopeId}/attachments
Apparently the request goes well because I get this return:
"attachments" => array:2 [
0 => array:4 [
"attachmentId" => "656A658D-AD83-438C-B58D-86738486349C"
"attachmentType" => "png"
"name" => "asdas"
"accessControl" => "SenderAndAllRecipients"
]
]
But nothing appears to the signers on the view. Where the signers can download or visualize the attachments that were added through the api?
UPDATE
The method to send the attachments:
public function addAttachments($saleContract, $envelopeId)
{
$attachments = array();
foreach ($saleContract->document as $document){
array_push($attachments, [
'accessControl' => 'senderAndAllRecipients',
'attachmentId' => $document->id,
'attachmentType' => \File::extension($document->storage_file_name),
'data' => base64_encode(Storage::disk('sienge')->get($document->storage_file_name)),
'name' => $document->file_name,
]);
}
Docusign::addEnvelopeAttachments($envelopeId, $attachments);
}
These are Envelope API Attachments and can be access only via API. When you add these attachments via API, then you can access via API only and will not be available on the Signing screen.
These are like metadata available in an envelope which should not be shown on the Signing screen, and can be accessed via API only. Dev Doc explains how to read these API attachments
I am retrieving a single row of data from several joined tabled using first(), this includes a json column called properties. I am able to display all the non json data as expected in my blade template.
However for the json column called properties I am pulling my hair out, spend endless hours googling, looking on her but to no avail.
Appreciate if someone could point out the blindingly obvious (as I'm sure it will be)
I have tried also and reached a point where I can access anything in the root of the object but nothing that is nested.
So pretty version of dd($quote->properties) returns;
"{
"mode": 1,
"service": 1,
"rates": {
"DAP": 825.22
},
"detail": {
"weights": {
"actual": 111.00,
"volume": 0,
"chargeable": 111.00
}
}
}"
I Appreciate the leading and trailing "s are only added to render in the browser and don't exist in the actual column.
Using {{ json_decode($quote->properties,true)['mode'] }}
Result = 1 which is as desired output.
However {{ json_decode($quote->properties,true)['rates']['DAP'] }}
Result = Undefined index: DAP
Desired = 825.22
{{ var_dump(json_decode($quote->properties,true)) }}
array (size=4)
'mode' => int 1
'service' => int 1
'rates' =>
array (size=1)
'DAP' => float 825.22
'detail' =>
array (size=1)
'weights' =>
array (size=3)
'actual' => string '111.00' (length=6)
'volume' => int 0
'chargeable' => string '111.00' (length=6)
Thanks all, I have no idea why working....
Perhaps I had a typo and re-adding fixed or cleared view cache??
Thank you all for responding.
I've defined a response format for data in that Laravel should return in the front-end with the following code:
$result1=$spaceRole->pluck('role','space.id')->all();
$resultToReturn=[];
foreach($result1 as $key=>$value){
$resultToReturn[$key]=array($value);
}
return $resultToReturn;
And the code gives me the following result as expected:
[
850 => [
"AUTHORS",
],
766 => [
"ADMINISTRATORS",
],
844 => [
"ADMINISTRATORS",
],
0 => [
"ADMINISTRATORS",
],
]
And i would like to keep the same format in my json response.But i the data returned in JSON,the format changes like this :
0: ["AUTHORS"]
1: ["ADMINISTRATORS"]
2: ["ADMINISTRATORS"]
3: ["ADMINISTRATORS"]
Now the indexes are 0,1,2,3.Where has 850,844,0 and 766 gone?I would like to use them and not those indexes?
Thanks
I've finally solved the problem.Casting my reponse with object type was the solution.
return (object)$resultToReturn; to keep my format
Try this:
$result1=$spaceRole->pluck('role','space.id')->all();
$resultToReturn=[]; foreach($result1 as $key=>$value){
$resultToReturn[$key]=array($value);
}
return json_encode($resultToReturn);
I am using Learning Locker (Learning Record Store).
I succeed inserting statements to it via the REST API.
But I did not succeed fetching statements from it.
How do I preform a query on statements? REST API?
I used tinCanPhp library. This is how you establish a connection with the Learning Locker database and query it in PHP. For example:
$lrs = new TinCan\RemoteLRS(
'endpoint/public/data/xAPI/',
'1.0.1',
'username',
'key'
);
$actor = new TinCan\Agent(
[ 'mbox' => 'mailto:dikla#gmail.com' ]
);
$verb = new TinCan\Verb(
[ 'id' => 'http://adlnet.gov/expapi/verbs/progressed' ]
);
$activity = new TinCan\Activity(
[ 'id' => 'http://game.t-shirt' ]
);
$statement = new TinCan\Statement(
[
'actor' => $actor,
'verb' => $verb,
'object' => $activity,
]
);
//get All Actor activity by his unique id
function getAllActorActivity($actorUri){
global $lrs;
$actor = new TinCan\Agent(
[ 'mbox' => $actorUri ]//actorUri should look like this 'mailto:dikla#gmail.com'
);
$answer=$lrs->queryStatements(['agent' => $actor]);
return $answer;
}
If it's via javascript you can use the ADL xAPI Wrapper. It simplifies communication with an LRS... https://github.com/adlnet/xAPIWrapper#get-statements
In general you do a GET request on endpoint /statements... try just that first and see if you get a json response with a "statements" and a "more" property. Then if that works, you can narrow down results with filters. See the spec for the details and options. https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#stmtapiget
try that curl command.. it should return a statement result albeit from the ADL LRS...
curl --user tom:1234 GET https://lrs.adlnet.gov/xapi/statements