I am working with Webhook to receive data, so I can store it into database, so I got data as Json and tried to decode it, so I can retrieve each one using json_decode and save them in a text file but I got nothing the file was empty
so here is me code :
<?php
require_once('vendor/autoload.php');
$payload = #file_get_contents('php://input');
if (isset($payload) && !empty($payload)) {
$data = json_decode($payload, true);
$myFile2 = "file.txt";
$myFileLink2 = fopen($myFile2, 'w+') or die("Can't open file.");
$newnbord = $data;
fwrite($myFileLink2, $newnbord);
fclose($myFileLink2);
}
and this is an example of data I received through webhook :
{"accountingCurrency":"USD","address1":"test address","billedCurrency":"EUR","billedRecurringPrice":"0.00","bin":"444444","cardType":"VISA","city":"testcity","country":"FR","initialPeriod":"30","lastName":"test","timestamp":"2020-11-12 06:05:49","username":"llllll","threeDSecure":"NOT_APPLICABLE"}
Related
I have setup a webhook from Pipedrive to activate on a deal being updated. I am struggling with the correct way to read the json response in php. I am new to Webhooks so is there another way of reading the response data. This is what I have:
<?php
$result = $_REQUEST['current']; // this is where it is not working I believe
$obj = json_decode($result, true);
$txt = $obj['id']
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
fwrite($myfile, $txt);
fclose($myfile);
In requestbin I am getting a response with all the correct information I am expecting
It appears that body of the event contains an array called "current" with all the relevant data in and I wanted to extract the "id" to see it working
"body": {
"current": {
"id": 71}
Any help really appreciated
I have found an answer that shows me what I require
$result = file_get_contents("php://input");
This pulls back the full response from the server showing data
You need to place this code first in your end file (which you shared with the live health API team.)
They will enable access to this file and once any report will submit, data will be sent on endfile.
you need to place this below code in that file and you will get a file created in the same root and will see data received from live health API.
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// fetch RAW input
$json = file_get_contents('php://input');
// decode json
$object = json_decode($json,true);
// expecting valid json
if (json_last_error() !== JSON_ERROR_NONE) {
die(header('HTTP/1.0 415 Unsupported Media Type'));
}
$time = time();
$fname ='_callback.test.txt';
file_put_contents($time.$fname , print_r($object, true));
$jdonFile = time().'_json_callback.test.txt';
$filename = $jdonFile;
$handle = fopen($filename, "w");
fwrite($handle, $json);
fclose($handle);
}
Using this code i was able to get data and i store this data into txt file. you can insert this data in sql database.
just copy and past the same code and your script will execute.
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// fetch RAW input
$json = file_get_contents('php://input');
// decode json
$object = json_decode($json,true);
// expecting valid json
if (json_last_error() !== JSON_ERROR_NONE) {
die(header('HTTP/1.0 415 Unsupported Media Type'));
}
$time = time();
$fname ='_callback.test.txt';
file_put_contents($time.$fname , print_r($object, true));
$jdonFile = time().'_json_callback.test.txt';
$filename = $jdonFile;
$handle = fopen($filename, "w");
fwrite($handle, $json);
fclose($handle);
}
I am creating a chatbot using Dialogflow. I need to use the webhook service. I have been able to successfully read the webhook request generated at my server using PHP. I am able to do use the data from webhook to do necessary stuff. But, I am not able to figure out the way to send JSON respone back to Dialogflow from PHP.
Code that I have tried:
<?php
$webhookContent = "";
$webhook = fopen('php://input' , 'rb');
while (!feof($webhook)) {
$webhookContent .= fread($webhook, 4096);
}
$data = json_decode($webhookContent);
$json = '{"fulfillmentMessages": [{"text": {"text": ["Text response from webhook"]}}]}';
$myfile = fopen("chatbot.txt", "a") or die("Unable to open file!");
fwrite($myfile, $webhookContent."\r\n");
fclose($myfile);
echo json_encode($json);
http_response_code(200);
?>
Above is a dummy of code that I am using. To send response I have tried the following options but nothing is working.
echo json_encode($json);
return json_encode($json);
return $json;
On Dialogflow it shows [empty response]
I have no knowledge of php, but from an iOS app I am trying to pass variables to json which can be accessible later, each time user complete level the post method push 3 variables and its value which adds that info into file like this
php code:
<?php
header ('Location: ');
$handle = fopen("data.json", "a");
foreach($_POST as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, ":");
fwrite($handle, $value);
fwrite($handle, "\r\n");
}
fwrite($handle, "\r\n");
fclose($handle);
exit;
?>
and I get the output like this after two users used post method:
Name:user1
Points:100
Level:1
Name:user2
Points:200
Level:2
can someone please help me to get json format output, with every time user push data with the post method it adds info to existing data instead of overwriting it?
I want output like this:
[
{"Name":"user1","Score":"100","Level":"1"},{"Name":"user2","Score":"200","Level":"2"}
]
<?php
$json = json_decode(file_get_contents("data.json"), true);
$json[] = json_encode($_POST);
file_put_contents("data.json", $json);
This decodes the current JSON file, then appends the new information and then saves over the existing JSON file.
Sorry if it is repeated question, already tried every single suggestion in google and StackOverflow.
For some reason, my response header always comes as Content-Type:text/html; charset=UTF-8.
I want content type to be json (Content-Type:application/json), Not sure what am I doing wrong. here is my code
<?php
header('Access-Control-Allow-Origin: *');
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/api/csvtojson.php');
require_once(__ROOT__.'/api/retrieve-login-data.php');
require_once(__ROOT__.'/api/access-control.php');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//Make sure that it is a POST request.
if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0){
throw new Exception('Request method must be POST!');
}
//Make sure that the content type of the POST request has been set to application/json
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if(strcasecmp($contentType, 'application/json') != 0){
throw new Exception('Content type must be: application/json');
}
//Receive the RAW post data.
$content = trim(file_get_contents("php://input"));
//Attempt to decode the incoming RAW post data from JSON.
$decoded = json_decode($content, true);
//If json_decode failed, the JSON is invalid.
if(!is_array($decoded)){
throw new Exception('Received content contained invalid JSON!');
}
$filename = $decoded['filename'];
// open csv file
if (!($fp = fopen($filename, 'r'))) {
die("Can't open file...");
}
//read csv headers
$key = fgetcsv($fp,"1024",",");
// parse csv rows into array
$json = array();
while ($row = fgetcsv($fp,"1024",",")) {
$json[] = array_combine($key, $row);
}
// release file handle
fclose($fp);
// encode array to json
header('Content-Type: application/json;charset=utf-8');
echo (json_encode($json, JSON_PRETTY_PRINT));
Response:
I had the same problem. This occurs because in one php file i had several
section.
======== File.php =========
<?php
//some code
?>
<?php
//some other code
?>
===========================
I've fixed it by mergin the code within the one tag only .
test this way :
ContentType : "application/x-www-form-urlencoded"
Please check if there is no warning before this line:
header('Content-Type: application/json;charset=utf-8');