This API is used by an access control device to report personnel pass-through records, After me (A third-party platform) called the API to a face recognition terminal or face recognition access control terminal.
Calling direction:
A face recognition terminal or face recognition access control terminal calls the API to a third-party platform.
Request description:
Request method: POST
Request URL: /LAPI/V1.0/System/Event/Notification/PersonVerification
Content-Type: text/plain
Request example:
{
"Reference": "204.2.1.20:5118/LAPI/V1.0/System/Event/Subscription/0",
"Seq": 5,
"DeviceCode": "210235C3R13202000093",
"Timestamp": 1564735558,
"NotificationType": 1,
"FaceInfoNum": 1,
"FaceInfoList": [
{
"ID": 5,
"Timestamp": 1564707615,
"CapSrc": 1,
"FeatureNum": 0,
"FeatureList": [
{
"FeatureVersion": "",
"Feature": ""
},
{
"FeatureVersion": "",
"Feature": ""
}
],
“Temperature”: 36.5,
“MaskFlag”: 1,
"PanoImage": {
"Name": "1564707615_1_86.jpg",
"Size": 101780,
"Data": "…"
},
"FaceImage": {
"Name": "1564707615_2_86.jpg",
"Size": 35528,
"Data": "…"
},
"FaceArea": {
"LeftTopX": 4981,
"LeftTopY": 3744,
"RightBottomX": 8250,
"RightBottomY": 5583
}
}
],
"CardInfoNum": 0,
"CardInfoList": [ ],
"GateInfoNum": 0,
"GateInfoList": [ ],
"LibMatInfoNum": 1,
"LibMatInfoList": [
{
"ID": 5,
"LibID": 3,
"LibType": 4,
"MatchStatus": 2,
"MatchPersonID": 0,
"MatchFaceID": 0,
"MatchPersonInfo": {
"PersonName": "",
"Gender": 0,
"CardID": "",
"IdentityNo": ""
}
}
]
}
I am using Laravel, I got [] when tried $request->all(), below when I dump( $request->getContent() );
dump
Seems like I need to remove "" from beginning and end of request in order to json_decode(), but no matter what string function I used - preg_replace, substring, etc - nothing changed, when I tried $content[0] I got { and NOT ", $content[-1] I got \n, $content[-2] I got }
Can anybody point out where I erred ?
Looks like this is the Content-Type header problem. But it should be a proper solution:
$find = ['/"""/', '/\\\n/', '/“/', '/”/'];
$replace = ['', '', '"', '"'];
$output = preg_replace($find, $replace, $your_input);
// now the $output is ready to be decoded
$decoded = json_decode($output);
$v = var_dump($decoded);
See this Repl for real implementation: Json Parse
Related
I came from Java-Script world and this syntax is strange for me for me.
I try to send post request from postman with body of array of objects like this:
My php code:
<?php
$signatures = $_POST["signatures"];
$signature = array_filter($signatures);
echo json_encode($signature);
But my response from postman is:
The way you are doing this is impossible I assume, you can only transfer strings using POST and GET, try to send all the data in just one KEY and then json_decode it in serve side. So try to send something like this
[
{
"id": 1,
"flowID": 1,
"fileID": 1,
"type": "partner",
"page": 1
}
]
as VALUE, with KEY: signatures, then in PHP code:
<?php
$signatures = json_decode($_POST["signatures"]); // <=== this is an array now
print_r($signature);
You are trying to filter an array but you did not written how to filter it (second parameter)
<?php
$signatures = $_POST["signatures"];
$signature = array_filter($signatures,<**variabile or function to be filtered**>);
echo json_encode($signature);
How to send array of objects:
Set postman to application/json and post body:
[
{
"id": 1,
"flowID": 1,
"fileID": 2,
"type": "identification",
"page": 1,
"positionX": 160,
"positionY": 241,
"sizeWidth": 150,
"sizeHeight": 250
},
{
"id": 2,
"flowID": 4,
"fileID": 2,
"type": "partner",
"page": 1,
"positionX": 160,
"positionY": 241,
"sizeWidth": 150,
"sizeHeight": 250
}
]
Get the request body and make it array:
$rawPostBody = file_get_contents('php://input');
$signatures = (array)json_decode($rawPostBody);
Loop over the array:
foreach($signatures as $signature) {
echo $signature->type;
}
Hope this helps those who ever look for how to do it.
I have a json data from the curl, then I want to send it to a view in codeigniter but when I display it appears an error "illegal string offset".
json data:
{
"ProgramId": "29aee327-cd89-48c0-b79f-69a0e2b03709",
"ProgramName": "Microsoft CSP",
"CountryFullName": "Indonesia",
"CurrencyCode": "IDR",
"ProductGroups": [
{
"GroupName": "Microsoft 365",
"Products": [
{
"ProductId": "3451a3b0-8cda-44a7-bad7-c30be81c4aaa",
"ProductName": "Microsoft 365 F1",
"ProductGroup": "Microsoft 365",
"ProductSku": "3451A3B0-8CDA-44A7-BAD7-C30BE81C4AAA",
"Price": 0,
"MinQty": 1,
"MaxQty": 10000000,
"Qty": 0,
"UnitPrice": 132300,
"ProductUnit": "Licenses",
"RetailPrice": 147000,
"CommitmentValue": null,
"TieredProductSku": null,
"ProductType": "NON-SPECIFIC",
"ProductDescription": "Designed to meet the needs of firstline workers in your organization. Offers best-in-class productivity across devices while providing IT with security and control.",
"ProductShortDescription": null,
"ProductRestrictions": null,
"IsTrialProduct": false,
"BillingCycleDuration": 1
}
],
"IsExpanded": true,
"Order": -10
}
then I retrieve the data from a controller to be sent to the display, here I use the file_get_contents function
public function index()
{
$this->template->add_includes('js', 'assets/js/holder.js');
$init = "";
$this->template->add_init($init);
// GET data
$source = base_url().'api/products/'.$this->tenantId;
$content = file_get_contents($source);
$result = json_decode($content, true);
//print_r($result); exit;
$data['products']=$result;
$this->template->load('templates/Template', 'Demo',$data);
}
Using the Gmail API call
https://www.googleapis.com/gmail/v1/users/userId/messages/id
return chats with label SENT
I have used the following code for getting the message content
$url = "https://www.googleapis.com/gmail/v1/users/$useremail/messages/$messageid?format=full";
$response = $gmailInstance->performHttpRequest($gmailAuthObj, $useremail, $url);
$responseBody = json_decode($response->getResponseBody(), TRUE);
I got the following response for the above API call
{
"id": msgid,
"threadId": threadid,
"labelIds": [
"CHAT",
"SENT"
],
"snippet": "sometext",
"historyId": 7 digit numeric,
"internalDate": "1371160444361",
"payload": {
"partId": "",
"mimeType": "text/html",
"filename": "",
"headers": [{
"name": "From",
"value": "Test User \u003ctuser#tdom.com\u003e"
}],
"body": {
"size": 20,
"data": somerandomstring
}
},
"sizeEstimate": 100
}
Does anybody know why the API response return chat message with SENT label in it? Is there a way to avoid this (SENT label from chat response)?
Im tring to show a list of articles on my site with a authour, date and comments field below the article so that users can see the number of comments for an article before even opening it.
The coments are from facebook and im using the graph api which returns the following JSON code per article, how do i get the total number of comments from this? Thanks
I have tried json_decode but the arrays am getting are all with count zero.
{
"http://www.withinzambia.com/technology-and-it/your-modem-isnt-that-fast.html": {
"comments": {
"data": [
{
"id": "10151004341202332_23086817",
"from": {
"name": "Cindi Mutale",
"id": "1045450015732187"
},
"message": "Glad someone finally pointed this out.",
"can_remove": false,
"created_time": "2012-07-02T19:46:58+0000",
"like_count": 0,
"user_likes": false
},
{
"id": "10151002332_23094740",
"from": {
"name": "Chanda Mike",
"id": "1000034452054679"
},
"message": "my modem is 7mbps, so that's not 7MB per second?",
"can_remove": false,
"created_time": "2012-07-03T13:51:24+0000",
"like_count": 0,
"user_likes": false
},
{
"id": "10151004341202332_23094782",
"from": {
"name": "Precious Chulu",
"id": "100242343243281187"
},
"message": "The max for the modem in the picture is 7mbps, which is actually about 900kb when you divide by 8, so you will never download at more than 1mb per second with these modems even when MTN or Airtel upgrades the network.",
"can_remove": false,
"created_time": "2012-07-03T13:57:56+0000",
"like_count": 0,
"user_likes": false
}
],
"paging": {
"next": "https://graph.facebook.com/10151004341202332/comments?value=1&redirect=1&limit=25&offset=25&__after_id=10151004341202332_23094782"
}
}
}
}
<?php
...
$count = 0;
$array = json_decode($input, true);
foreach($array AS $website) {
$count += count($website['comments']['data']);
}
...
?>
$count is answer.
Bonus :)
$jsonArr = ' ..your JSON array.. ';
$decodedArr = json_decode($jsonArr);
$num_comments = count($decodedArr->{'http://www.withinzambia.com/technology-and-it/your-modem-isnt-that-fast.html'}->comments->data);
echo $num_comments;
tested and works.
be aware that if you load your JSON into a string, like i did here, single quotes will need to be escaped.
How would I go about selecting the data of each title from the following JSON?
I have the JSON decoded, but I'm not sure how to select the part I want.
{
"responseData": {
"results": [
{
"title": "Justin Giesbrecht 749",
"titleNoFormatting": "Justin Giesbrecht 749",
},
{
"title": "Gopher dunes 09",
"titleNoFormatting": "Gopher dunes 09",
},
{
"title": "dirtbike Justin",
"titleNoFormatting": "dirtbike Justin",
},
{
"title": "A Warming",
"titleNoFormatting": "A Warming",
}
],
"cursor": {
"pages": [
{
"start": "0",
"label": 1
},
{
"start": "4",
"label": 2
}
],
"estimatedResultCount": "6",
"currentPageIndex": 0,
}
},
"responseDetails": null,
"responseStatus": 200
}
I thought it would be something like this, but I don't get anything:
echo "Response ". $jsonS->responseData->results[1]->title;
Actually you've got the reading of the title part right, it's the JSON that is invalid.
Copying the JSON into a JSON validator/lint e.g. http://www.jsonlint.com/ will show that the you have additional , (commas) after the last object attribute in a few places (5 places to be exact, after each 'titleFormatting' attribute and after 'currentPageIndex').
If you fix those errors and parse it using json_decode e.g.:
$jsonS = json_decode($json_text);
Then your own code:
echo "Response " . $jsonS->responseData->results[1]->title;
Will output the second (index 1 being the second index) results title
Response Gopher dunes 09
When you json_decode something it is converted to a regular PHP array, so you can reference it like $decodedObject["nodeName"].
When I parse that JSON as you quoted it with PHP's json_decode method, it gives me NULL because PHP doesn't think it's valid JSON. Add a var_dump($jsonS); to see if this is happening to you as well. If it is, you may need to be sure your JSON is valid.
Here's what I did, for reference:
$json_data = <<<END_OF_JSON
{
"responseData": { "results": [
{
"title": "Justin Giesbrecht 749", "titleNoFormatting": "Justin Giesbrecht 749",
},
{
"title": "Gopher dunes 09",
"titleNoFormatting": "Gopher dunes 09",
},
{
"title": "dirtbike Justin",
"titleNoFormatting": "dirtbike Justin",
},
{
"title": "A Warming",
"titleNoFormatting": "A Warming",
}
],
"cursor": {
"pages": [ {
"start": "0",
"label": 1
},
{
"start": "4",
"label": 2
}
],
"estimatedResultCount": "6",
"currentPageIndex": 0,
}
},
"responseDetails": null,
"responseStatus": 200
}
END_OF_JSON;
$jsonS = json_decode($json_data);
var_dump($jsonS);
echo "Response ". $jsonS->responseData->results[1]->title;
And the output was:
NULL Response
If you're using different JSON, please edit your question and share it. But if you're actually using the above, it's not valid...
In JSON you are not allowed to leave trailing comma in array/object definition. So, when in PHP is perfectly valid to write:
$a = array(1,2,3,);
(note last comma), in JSON
a : [1,2,3,]
or
a : {x :'y',}
is invalid.