Pipedrive Webhook PHP endpoint code example - php

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

Related

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

send json response back to webhook caller

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]

Recieving Blank/No Response to Status Update Notification(statusUpdateNotification) from Apple Server - PHP v5.4.43

We have set up a URL in the app profile in iTunes and our server has cleared the ATS security criteria.
Following are the codes that we have tried to implement :
$data = json_decode(file_get_contents('php://input'), true);
$fp = fopen('appdata.txt', 'a');
fwrite($fp, $data);
fclose($fp);
We got no response with this code.
Then we tried -
$data = print_r($_REQUEST, TRUE);
$fp = fopen('appdata.txt', 'a');
fwrite($fp, $data);
fclose($fp);
We get a blank array in our 'appdata.txt' file as -
Array
(
)
Is there any way to find out if we are even receiving a response from Apple server?
Finally, we have solved the issue :)
While checking error log file we find this error message -
PHP Warning: "fwrite() expects parameter 2 to be string, array given."
So we replaced this line in our first code -
fwrite($fp, $data);
with this -
fwrite($fp, print_r($data, true));
Now the code is working fine and we are getting the Status Update Notification every time.

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