send json response back to webhook caller - php

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]

Related

Pipedrive Webhook PHP endpoint code example

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

Json decode returns nothing

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"}

My PHP IPN Script is not working - what's causing it to fail?

Hello StackOverflow Community!
I've been working on my PayPal IPN Script for quite a while, but without much success. This script is supposed to return VALID and put it in stats.txt, but for some reason this is not happening, and instead it is not working at all. I've used PayPal sandbox and the foreach POST seems to be working fine. I've tried putting the raw URL into my browser, with all the GET values, but for some reason paypal just gives me a timeout error, and doens't even return a INVALID. Here is my code:
// Get POST data and refine the GET value.
foreach ($_POST as $name => $value) {
$postdata = $name.'='.$value.'&';
$result .= substr('?'.$postdata, 0, -1);
}
// Verify that payment was really made.
$verify = file_get_contents('https://ipnpb.sandbox.paypal.com/cgi-bin/webscr/'.$result.'');
if ($verify == "VERIFIED") {
// Any code run here will only be run if payment is verified.
echo 'Payment success!';
$data = "Payment made successfully.";
$fp = fopen('stats.txt', 'a');
fwrite($fp, $data);
}
elseif ($verify == "INVALID") {
echo 'Payment Declined.';
$data = "The payment isn't valid.";
$fp = fopen('stats.txt', 'a');
fwrite($fp, $data);
}
else {
echo 'An unexpected error occured.';
$data = "A timeout error occured.";
$fp = fopen('stats.txt', 'a');
fwrite($fp, $data);
}
// Let PayPal know that the IPN was successful.
header("HTTP/1.1 200 OK");
After payment, in stats.txt, the text I'm getting is: A timeout error occured. By the way, the bit at the top, what it's doing is its getting the POST data then formatting it into a GET url (eg: ?value1=text1&value2=text2)
Thanks for your help!

PHP: Adding entries from php to json

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.

Using PHP to store results of a post request

Im currently working with an API which requires we send our collection details in xml to their server using a post request.
Nothing major there but its not working, so I want to output the sent xml to a txt file so I can look at actually whats being sent!!
Instead of posting to the API im posting to a document called target, but the xml its outputting its recording seems to be really wrong. Here is my target script, note that the posting script posts 3 items, so the file being written should have details of each post request one after the other.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// get the request data...
$payload = '';
$fp = fopen('php://input','r');
$output_file = fopen('output.txt', 'w');
while (!feof($fp)) {
$payload .= fgets($fp);
fwrite($output_file, $payload);
}
fclose($fp);
fclose($output_file);
?>
I also tried the following, but this just recorded the last post request so only 1 collection item was recorded in the txt file, instead of all 3
output_file = fopen('output.txt', 'w');
while (!feof($fp)) {
$payload .= fgets($fp);
}
fwrite($output_file, $payload);
fclose($fp);
fclose($output_file);
I know im missing something really obvious, but ive been looking at this all morning!
Change
$output_file = fopen('output.txt', 'w');
to
$output_file = fopen('output.txt', 'a');
Also, change
$payload .= fgets($fp);
to
$payload = fgets($fp);
you should probably use curl instead to fetch the content and then write it via fwrite
change
$payload .= fgets($fp);
to
$payload = fgets($fp);

Categories