How to write Boolen json to Data in PHP - php

I have json, and i want write boolen of vpn to file.
{
"ip": "113.169.100.228",
"security": {
"vpn": false,
"proxy": false,
"tor": false
},
"location": {
"city": "",
"region": "",
"country": "Vietnam",
"continent": "Asia",
"region_code": "",
"country_code": "VN",
"continent_code": "AS",
"latitude": "16.0020",
"longitude": "105.9984",
"time_zone": "Asia/Vientiane",
"locale_code": "en",
"metro_code": "",
"is_in_european_union": false
},
"network": {
"network": "113.160.0.0/11",
"autonomous_system_number": "AS45899",
"autonomous_system_organization": "VNPT Corp"
}
}
How to write "vpn": false to php?
I use , it don't write to to file data.html
<?php
$status = $details_3->security->vpn;
$fp = #fopen('data.html', "a");
if (!$fp) {
echo 'error';
}
else
{
fwrite($fp, $status);
fwrite($fp, "\r\n");
fclose($fp);
}
?>
Please help me, when i echo $status , it can show, but don't write to file

When you convert true and false to strings, they become "1" and "". If you want to write something else, you have to do the conversion yourself, e.g.
fwrite($fp, $status ? "true" : "false");

Related

What format is this PHP data in?

I'm trying to work out what format this data is in, so i can change it. It's just horrible.
a:20:{s:12:"tagline_text";s:0:"";s:8:"gAddress";s:40:"380 North Shore Drive, Pittsburgh, 15212";s:8:"latitude";s:9:"40.445917";s:9:"longitude";s:10:"-80.011324";s:5:"phone";s:0:"";s:5:"email";s:0:"";s:7:"website";s:44:"http://tequilacowboy.com/pittsburgh/wannabs/";s:7:"twitter";s:0:"";s:8:"facebook";s:0:"";s:8:"linkedin";s:0:"";s:11:"google_plus";s:0:"";s:7:"youtube";s:0:"";s:9:"instagram";s:0:"";s:5:"video";s:0:"";s:7:"gallery";N;s:12:"price_status";s:0:"";s:10:"list_price";s:0:"";s:13:"list_price_to";s:0:"";s:15:"claimed_section";s:0:"";s:4:"faqs";a:2:{s:3:"faq";a:1:{i:0;s:0:"";}s:6:"faqans";a:1:{i:0;s:0:"";}}}
Looks like the output of PHP serialize() to me. You may be able to decode it back into an array by writing a PHP script, read the data from its file or whatever, and use unserialize() on it.
I tested this guess:
<?php
// read $string from a file or something
print json_encode(unserialize($string));
Run this PHP code:
php myscript.php | jq .
Output:
{
"tagline_text": "",
"gAddress": "380 North Shore Drive, Pittsburgh, 15212",
"latitude": "40.445917",
"longitude": "-80.011324",
"phone": "",
"email": "",
"website": "http://tequilacowboy.com/pittsburgh/wannabs/",
"twitter": "",
"facebook": "",
"linkedin": "",
"google_plus": "",
"youtube": "",
"instagram": "",
"video": "",
"gallery": null,
"price_status": "",
"list_price": "",
"list_price_to": "",
"claimed_section": "",
"faqs": {
"faq": [
""
],
"faqans": [
""
]
}
}

Get value from nested json php

I am receiving data from a webhook with the following code below.
<?php
$inputJSON = file_get_contents('php://input');
$input = json_decode($inputJSON, TRUE);
$myfile = fopen("callback.txt", "a") or die("Unable to open file!");
$txt = $input["payload"]["type"];
fwrite($myfile, "\n". $txt);
fclose($myfile);
http_response_code(200);
?>
I am trying to get the 'type' value from the retuned output. I have found that using an if statement inside a for each loop would do the job. However i'm sure this isn't an ideal solution. is there a more direct way of getting that element?
The code above is outputting an empty text file.
and the documentation shows the json should be in the following format:
{
"action": "add",
"collection": "broadcast",
"payload": {
"author": "Sveninge Bambuser",
"created": 1474033783,
"customData": "",
"height": 540,
"id": "9353eaec-794f-11e6-97c0-f19001529702",
"ingestChannel": "cfc8626c-9a0e-ab78-6424-3eb0978d8e45",
"lat": 63.205312,
"length": 0,
"lon": 17.13011,
"positionAccuracy": 25,
"positionType": "GPS",
"preview": "https://archive.bambuser.com/9353eaec-794f-11e6-97c0-f19001529702.jpg",
"resourceUri": "https://cdn.bambuser.net/broadcasts/9353eaec-794f-11e6-97c0-f19001529702?da_signature_method=HMAC-SHA256&da_id=9353eaec-794f-11e6-97c0-f19001529702&da_timestamp=1474033783&da_static=1&da_ttl=0&da_signature=eaf4c9cb29c58b910dcbad17cf7d8a3afa4e6a963624ba4c4fd0bb5bade1cdd6",
"tags": [
{
"text": "whoa"
}
],
"title": "Amazing!",
"type": "live",
"width": 960
},
"eventId": "93df93061a891c23"
}
I replaced:
$txt = $input["payload"]["type"];
with:
$txt = $input['payload']['type'];
it appears to be the double quotes causing the issue.

Getting data from json and passing to url

Want to send the json data to my url, when the json is received i receive all the json data on the url and not the data from 'resolvedQuery'
$update_response = file_get_contents("php://input");
$update = json_decode($update_response, true);
$results = $update['result']['resolvedQuery'];
$url = "https://autoremotejoaomgcd.appspot.com/sendmessage?key=APA91bEq&message=Data" . urlencode(json_encode($update));
Json Data
{
"id": "4ed272a3-b9a4-4f18-a67e-10e12e4f4149",
"timestamp": "2017-12-10T10:07:54.282Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "turn livingroom light on",
"action": "",
"actionIncomplete": false,
"parameters": {
"power-toggle": "on",
"room": "livingroom"
},
"contexts": [],
"metadata": {
"intentId": "19764ecc-715c-4b25-960f-9845f2aef1f9",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false",
"webhookResponseTime": 503,
"intentName": "lights"
},
"fulfillment": {
"speech": "Ok, Turning livingroom light on",
"messages": [
{
"type": 0,
"speech": "Ok, Turning livingroom light on"
}
]
},
"score": 1
},
"status": {
"code": 206,
"errorType": "partial_content",
"errorDetails": "Webhook call failed. Error: Webhook response was empty.",
"webhookTimedOut": false
},
"sessionId": "395d3517-05da-42ac-9037-dadac519ab0b"
}
How do i get data from 'resolvedQuery' and send it to my URL
In the code you provided, you have the following line of code
$url = "https://autoremotejoaomgcd.appspot.com/sendmessage?key=APA91bEq&message=Data"
. urlencode(json_encode($update));
You are appending to the URL parameter message the results of json_decode($update_response, true). $update_response contains all the decoded JSON data posted by the HTTP request which is not what you want.
I believe what you want is a new parameter called resolved-query to be set to the results of json_decode($result, true).
Try chaninging your $url variable to the following:
$url = "https://autoremotejoaomgcd.appspot.com/sendmessage?key=APA91bEq&message=Data&resolved-query="
. urlencode(json_encode($result));
URL will be set to the following string:
https://autoremotejoaomgcd.appspot.com/sendmessage?
key=APA91bEq&
message=Data&
resolved-query=turn+livingroom+light+on

smsgateway.me store message status in php variable

Using smsgateway.me to send a message I use the default code with my own variables which are working fine. However, sometimes the SMS does not send and I would like to store the SMS in the database if failed.
To do so I think I need a value from the $result array, but I dont know how to start. I tried many different codes without any result.
Can anywone please tell me how to get an if sent ok -> do something / else do something else based on the code and variables below?
Thanks a lot!
Documentation here
Here is the default code (with default variables) that sends a message:
<?php
include "smsGateway.php";
$smsGateway = new SmsGateway('demo#smsgateway.me', 'password');
$deviceID = 1;
$number = '+44771232343';
$message = 'Hello World!';
$options = [
'send_at' => strtotime('+10 minutes'), // Send the message in 10 minutes
'expires_at' => strtotime('+1 hour') // Cancel the message in 1 hour if the message is not yet sent
];
//Please note options is no required and can be left out
$result = $smsGateway->sendMessageToNumber($number, $message, $deviceID, $options);
?>
Success content:
{
"success": true,
"result": {
"success": [
{
"id": "308",
"device_id": "4",
"message": "hello world!",
"status": "pending",
"send_at": "1414624856",
"queued_at": "0",
"sent_at": "0",
"delivered_at": "0",
"expires_at": "1414634856",
"canceled_at": "0",
"failed_at": "0",
"received_at": "0",
"error": "None",
"created_at": "1414624856",
"contact": {
"id": "14",
"name": "Phyllis Turner",
"number": "+447791064713"
}
}
],
"fails": [
]
}
}
error content:
{
"success": true,
"result": {
"success": [
],
"fails": [
"number": "+44771232343"
"message": "hello world!",
"device": 1
"errors": {
"device": ["The selected device is invalid"],
}
]
}
}
This did the trick if failed:
if (isset($result['response']['result']['fails'][0]['number']) === TRUE) {
echo 'failed';
else{
echo 'success';
}
this works on success:
if(isset($result['response']['result']['success'][0]['id']) === TRUE){
echo 'success';
}else{
echo 'failed';
}
Thanks Michael, your reply helped me to change it a bit and figure it out!

file_get_contents not getting json

I have the PHP which should get the JSON object from the postcodes api (http://postcodes.io/) however it does not seem to be working.
if(isset($_POST["postcode"])){
$json = file_get_contents('api.postcodes.io/postcodes/'.$_POST["postcode"]);
$json_data = json_decode($json, true);
echo "My token: ". $json_data["status"];
}
the $json_data["status"]; does not echo anything.
for example when $_POST["postcode"] equals "IP12 2UH" the below should be returned:
{
"status": 200,
"result": {
"postcode": "IP12 2UH",
"quality": 1,
"eastings": 633715,
"northings": 253024,
"country": "England",
"nhs_ha": "East of England",
"longitude": 1.41299500121177,
"latitude": 52.126073695307,
"parliamentary_constituency": "Suffolk Coastal",
"european_electoral_region": "Eastern",
"primary_care_trust": "Suffolk",
"region": "East of England",
"lsoa": "Suffolk Coastal 007G",
"msoa": "Suffolk Coastal 007",
"nuts": null,
"incode": "2UH",
"outcode": "IP12",
"admin_district": "Suffolk Coastal",
"parish": "Suffolk Coastal",
"admin_county": "Suffolk",
"admin_ward": "Rendlesham",
"ccg": "NHS Ipswich and East Suffolk",
"codes": {
"admin_district": "E07000205",
"admin_county": "E10000029",
"admin_ward": "E05007216",
"parish": "E04009449",
"ccg": "E38000086"
}
}
}
You need to add http:// in the url

Categories