How to read context parameters using php webhook in dialogflow - php

Need help with the following I writing a webhook in php, and need the ability to read the context parameters.
Can someone help me to understand how it can be done?
Here is my example JSON:
{
"id": "6e774dc2-2323-42b3-bd3c-ab64930f8b92",
"timestamp": "2017-12-22T21:12:19.094Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "Yes",
"action": "Triage.Triage-yes",
"actionIncomplete": false,
"parameters": {},
"contexts": [
{
"name": "triage-followup",
"parameters": {
"triagecriteria": [],
"roomEntity.original": "",
"roomname": "300",
"roomnames.original": "living",
"roomid": "200",
"context": "",
"roomnames": [
"living"
],
"counter": "400",
"roomEntity": "100",
"triagecriteria.original": ""
},
"lifespan": 3
}
],
"metadata": {
"intentId": "ecd4a2e5-65a0-41b2-ac72-edcf4d2e73f2",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false",
"webhookResponseTime": 203,
"intentName": "Triage - yes"
},
"fulfillment": {
"speech": "Yes",
"source": "agent",
"displayText": "No",
"messages": [
{
"type": 0,
"speech": "Yes"
}
]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success",
"webhookTimedOut": false
},
"sessionId": "db8c1a4e-fa0c-4257-a536-78b63879eef9"
}
I want to be able to refer to [results][Contexts][Parameters]
I am using
$update_response = file_get_contents("php://input");
$update = json_decode($update_response, true, 512, JSON_BIGINT_AS_STRING);
and trying to access the value as $update["results"]["Contexts"]["Parameters"]["roomid"]

The names are case sensitive, and you need to be careful about trailing "s"es. Since some of the items are numerically indexed arrays, you'll need to include that as part of the index. Try
$update["result"]["contexts"][0]["parameters"]["roomid"]

Related

How to read Specific value from curl Response

this is my response
i need to read first line id value & url of sandbox
{
"id": "chg_g5Y25220190539Ob590811651",
"object": "charge",
"live_mode": false,
"api_version": "V2",
"method": "CREATE",
"status": "INITIATED",
"amount": 1.000,
"currency": "KWD",
"threeDSecure": false,
"card_threeDSecure": false,
"save_card": false,
"statement_descriptor": "Sample",
"description": "Test DESC",
"transaction":
{
"timezone": "UTC+03:00",
"created": "1573191592651",
"url": "https://sandbox.payments.tap.company/test_gosell/v2/payment/response.aspx?tap_chg=PsV8hZdr78kEIAOZl9XL1B5u3dXe%2bBQ%2b3Hrd%2fempxYs%3d&sess=noWi606EI%2bM%3d&token=PsV8hZdr78kEIAOZl9XL1B5u3dXe%2bBQ%2bTyYdQy9iGpS6aAXwKOqAdw%3d%3d",
"expiry": { "period": 30, "type": "MINUTE" },
"asynchronous": false
},
"reference": { "transaction": "txn_0001", "order": "ord_0001" },
"response": { "code": "100", "message": "Initiated" },
"receipt": { "email": true, "sms": true },
"customer": { "first_name": "yxy", "last_name": "ttt", "email": "xyx#xyz.com" },
"source": { "object": "source", "id": "src_card" },
"redirect": { "status": "PENDING", "url": "http://localhost:81/School/" },
"post": { "status": "PENDING", "url": "http://localhost:81/School/" } }
assuming you have your response from curl in a variable called $resp
$resp_decoded = json_decode($resp,TRUE);
print($resp_decoded['id']);
print($resp_decoded['transaction']['url']);

Google Analytics API in inserting goals using PHP

I need help regarding the returned error after initiating request:
Here's the request body:
{
"name": "Test Leads",
"id": "2",
"type": "Event",
"active": true,
"eventDetails": {
"eventConditions": [
{
"type": "Category",
"comparisonType": "EQUAL",
"expression": "Leads"
},
{
"type": "ACTION",
"comparisonType": "EQUAL",
"expression": "Create"
}
],
"useEventValue": true
}
}
Error:
{"error":{
"errors":[
{"domain":"global","reason":"backendError","message":"Backend Error"}
],
"code":500,
"message":"Backend Error"
}
}
Expected Goal Details when successfully posted:
https://www.screencast.com/t/5QOJ28Tnk
Can someone check the code and see what's wrong or missing?
Thanks.
"name": "Test Leads",
"kind": "analytics#goal",
"id": "2",
"type": "Event",
"active": true,
"eventDetails": {
"eventConditions": [
{
"type": "CATEGORY",
"matchType": "EXACT",
"expression": "Leads"
},
{
"type": "ACTION",
"matchType": "EXACT",
"expression": "Create"
}
],
"useEventValue": true
}
}```

PHP - Retrieve JSON data and values [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I'm new into PHP and JSON and I have a problem, I want to retrieve a item and value from a JSON:
{
"status": true,
"webhook_type": 100,
"data": {
"product": {
"id": "lSEADIQ",
"attachment_id": null,
"title": "Registration",
"description": null,
"image": null,
"unlisted": false,
"type": "service",
"price": 1,
"currency": "EUR",
"email": {
"enabled": false
},
"stock_warning": 0,
"quantity": {
"min": 1,
"max": 1
},
"confirmations": 1,
"custom_fields": [
{
"name": "Forum username",
"type": "text",
"required": true
}
],
"gateways": [
"Bitcoin"
],
"webhook_urls": [],
"dynamic_url": "",
"position": null,
"created_at": "2018-10-01 12:51:12",
"updated_at": "2018-10-01 12:55:46",
"stock": 9223372036854776000,
"accounts": []
},
"order": {
"id": "8e23b496-121a-4dc6-8ec4-c45835680db2",
"created_at": "Tue, 02 Oct 2018 00:54:56 +0200",
"paid_at": null,
"transaction_id": null,
"confirmations": 1,
"required_confirmations": 3,
"received_amount": 0,
"crypto_address": "1NeNQws7JLbTr6bjekfeaXSV7XiyRsv7V8",
"crypto_amount": "0.4815",
"quantity": 1,
"price": 19.99,
"currency": "EUR",
"exchange_rate": "1.21",
"gateway": "BTC",
"email": "webhook#site.gg",
"ip_address": "123.456.789.111",
"agent": {
"geo": {
"ip": "214.44.18.6",
"iso_code": "US",
"country": "United States"
},
"data": {
"is_mobile": false,
"is_table": false,
"is_desktop": true,
"browser": {
"name": "Chrome",
"version": "63.0.3239.132"
}
}
},
"custom_fields": [
{
"name": "user_id",
"value": 184191
}
],
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3)"
}
}
}
I want to retrieve items from data -> order, for example "id" or "ip_address".
Thank you for read this, I hope someone can help me in this, because I'm lost, I started to code very recently and I'm trying to learn a lot.
Regards!
Where test.json is the json you uploaded, place it in a file named test.json and ensure its placed in the same directory.
<?php
$load = file_get_contents("test.json") or die("JSON load failed");
$json_a = json_decode($load, true);
print $json_a['data']['order']['ip_address'] . "\n";
?>
Gives:
123.456.789.111
My answer reads the JSON from a file as were it dumped directly in your code, which indeed it could be, it would make the code less readable and your file more messy.
If you dont want to place the file in the same directory, simply specify the full file path. E.g. file_get_contents("this/dir/here/test.json");
You can read about how json_decode works here, its essential we pass it the true parameter to make our arrays associative.
You can extract your need array from JSON data. You can use a loop too to read all your data inside the order array.
$array = json_decode($json, true);
$verbose = $array['data'];
$orderArray = $verbose['order'];
print_r($orderArray);
echo $orderArray['id'];
echo $orderArray['ip_address'];

REGEX: needed in php [duplicate]

This question already has an answer here:
How to extract individual elements from a JSON string?
(1 answer)
Closed 5 years ago.
{
"status": "success",
"data": {
"title": "test2",
"json_query": {
"condition": "AND",
"rules": [{
"id": "event",
"field": "event",
"type": "string",
"operator": "equal",
"value": "signup"
}, {
"condition": "AND",
"rules": [{
"id": "event",
"field": "event",
"type": "string",
"operator": "equal",
"value": "signup"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "epub"
}]
}]
}, {
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "arts"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "automotive"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "books"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "business"
}, {
"condition": "AND",
"rules": [{
"id": "kwd",
"field": "kwd",
"type": "string",
"operator": "equal",
"value": "books"
}]
}]
}]
}]
}]
}
}
}
This is my string and i need the regex which will get all id with its value
eg: "id" : "event", "id" : "event", "id" : "kwd", "id" : "kwd"
means every occurrence, so i can replace or add a unique value to id
NOTE:- I dont want to make an array, dont want to do with loop,
finding possibility to replace with preg_match or any other regex method.
I got my answer myself:
preg_match_all('/"id":"(.*)",/U', $json, $matches, PREG_PATTERN_ORDER);
print_r($matches); die;

How can i get json object results in php using php_decode and url

below is my json object result of google api now i want to get url of cover from it which is parameter 'thumbnail' in below json object,but i dont know how can i retrive it can anyone here help me out?
{
"kind": "books#volumes",
"totalItems": 1,
"items": [
{
"kind": "books#volume",
"id": "9VKCkifBlisC",
"etag": "hhhZjjV6arI",
"selfLink": "url",
"volumeInfo": {
"title": "Instructor's manual to accompany An introduction",
"authors": [
"Jean-Paul Tremblay",
"P. G. Sorenson"
],
"publishedDate": "1976",
"industryIdentifiers": [
{
"type": "ISBN_10",
"identifier": "0070651515"
},
{
"type": "ISBN_13",
"identifier": "9780070651517"
}
],
"pageCount": 278,
"printType": "BOOK",
"categories": [
"Computers"
],
"averageRating": 5.0,
"ratingsCount": 1,
"contentVersion": "0.0.1.0.preview.0",
"imageLinks": {
"smallThumbnail": "url",
/*this is what i need*/
"thumbnail": "url"
},
"language": "en",
"previewLink": "url",
"infoLink": "url",
"canonicalVolumeLink": "url"
},
"saleInfo": {
"country": "IN",
"saleability": "NOT_FOR_SALE",
"isEbook": false
},
"accessInfo": {
"country": "IN",
"viewability": "NO_PAGES",
"embeddable": false,
"publicDomain": false,
"textToSpeechPermission": "ALLOWED",
"epub": {
"isAvailable": false
},
"pdf": {
"isAvailable": false
},
"webReaderLink": "url",
"accessViewStatus": "NONE",
"quoteSharingAllowed": false
}
}
]
}
You could do like this..
$arr = json_decode($json,true);
echo $arr['items'][0]['volumeInfo']['imageLinks']['thumbnail'];
Demo

Categories