Read Google Calendar API Push Notification in PHP - php

I have successfully configured Google API Calendar Event Change Push Notifications. My notification page gets called as it should be when I set up the notification or change the calendar.
BUT...being a bit of a PHP dunce I don't understand how to see the content of the notification.
SO (MAIN QUESTION)...how do I get at the variables in the notification? The notification calls my designated page. What are the variables? How are they sent? How are they organized?
There is the larger question of how one should determine the content of a call to a page in PHP. I tried all manner of var_dump, print_r, etc. All I get with:
<?php
$arr = get_defined_vars();
error_log(print_r($arr),0);
?>
is: 1 Yes, just the number 1 in my error_log.

The way I read the respose is:
$content = apache_request_headers();
If you print $content the data is self explenatory. However; for more information hwo to read the header check the link below:
https://developers.google.com/google-apps/calendar/v3/push#receiving

Thanks. it's working. i wrote to file.
$content = apache_request_headers();
get_contents($content);
function get_contents($result) {
$fp = fopen('config.json', 'w');
fwrite($fp, json_encode($result));
fclose($fp);
}

Related

How do I get data from Webhook to store in a database?

I am working with a Webhook based of of SendGrid's API v3. The Webhook is completely set up with an endpoint that SendGrid posts to, but I need to be able to receive that parsed data and store it in a database using PHP, but do not know how to do that.
I have worked with APIs before where I initiate the retrieval or POST of data, but when the API server is the one POSTing, how do I catch the data being parsed through the Webhook endpoint? I have some simple thus far, but am really unclear of how to tackle this:
<?php
$json = file_get_contents('https://example.com/webhook.php');
$json_decoded = json_decode($json, true);
$var_dump(json_decoded);
?>
I have tried sending multiple emails to the host, but every time I make this call I return NULL.
Try using the code from this example. This will give you the raw HTTP request body bytes to decode.
<?php
$data = file_get_contents("php://input");
$events = json_decode($data, true);
foreach ($events as $event) {
// Here, you now have each event and can process them how you like
process_event($event);
}
?>
More info on php://input

Dropbox API PHP - notification request is empty

I have what should be a very simple issue to solve, but I can't figure out what is going wrong.
Just started a project to use the new Dropbox API v2 to receive notifications for file/folder changes. Following the steps provided in the documentation provided, but I run into an issue right off the bat.
I've verified the webhook, and I do receive a POST request from Dropbox every time a file is changed, but the POST request just contains an empty array. The code is simple, as I have just begun the project:
// USED for initial verification
/*
$challenge = $_GET['challenge'];
echo $challenge;
*/
$postData = $_POST;
$post_dump = print_r($postData, TRUE);
$fpost = fopen('postTester.txt', 'w');
fwrite($fpost, $post_dump);
fclose($fpost);
$postData is an empty array with sizeOf() 0.
Any ideas?
Here is the updated code with the solution. Very simple fix.
$postData = file_get_contents("php://input");
$post_dump = print_r($postData, TRUE);
$fpost = fopen('postTester.txt', 'w');
fwrite($fpost, $post_dump);
fclose($fpost);
I believe this is because $_POST is only for application/x-www-form-urlencoded or multipart/form-data Content-Types. The payload delivered by Dropbox webhooks is application/json.
It looks like you'll instead want to use $HTTP_RAW_POST_DATA or php://input, depending on your version of PHP.
You can get the raw payload and then json_decode it to get the structured information.

Receiving JSON in php, and using data

I have been trying to set up a Shopify webhook (documentation here) through the Shopify admin section. So far I have not been able to grab any of the data being sent in a test webhook using multiple methods, but I know it is sending because I created a Requestbin and have been seeing data come through.
I found this chunk of code here on Stackoverflow.
header('Access-Control-Allow-Origin: *');
$postdata = file_get_contents("php://input");
$file = "log.txt";
$current = file_get_contents($file);
$current .= $postdata;
file_put_contents($file, $current);
echo $current;
So far it is the only code I have been able to use to actually see any JSON. But Im not wanting to write the JSON to the "log.txt" file every time the webhook is fired. But once I try to remove or adjust the code in anyway I no longer see any JSON. It seems that I have to write $postdata to a file, and then retrieve the contents to get an array.
Is is possible to access the JSON without having to first write it to another file?
So to begin, my fundamental thought process was flawed on how webhooks worked.
header('Access-Control-Allow-Origin: *');
$postdata = file_get_contents("php://input");
$file = "log.txt";
$current = file_get_contents($file);
$current .= $postdata;
file_put_contents($file, $current);
echo $current;
The reason this code needed to write the contents of file_get_contents("php://input") to the text file was because the POST request sent by the Shopify webhook, only hits the page briefly. As soon as you refresh the page, you lose all the POST data...seems obvious I know. That said I needed a way to determine if the JSON Shopify is sending out is working all fine on my server. So I created this...
$json = json_decode(file_get_contents("php://input"));
file_put_contents('./realRequests.txt', $json->id . "\n", FILE_APPEND);
What this does is capture the ID of an order everytime Shopify fires the webhook, and wrties it to a new line in a requests file. That way you know you are able to recive working code from shopify.

Processing POST data from a third party

I have been struggling with the following problem for few days. I am expected to receive POST data from a third party company for certain real time events. I have been in contact with them as to why my script isn't seeing anything. Unfortunately, they are less then helpful and just tell me "it works for everybody else".
I setup a simple PHP script after doing some research on the web but it always shows no post data. This isn't my area of expertise so I may be missing something obvious.
Here is the only documentation they give:
This API will send a real-time http request upon every successful event with all of the conversion data. The data is sent via an http POST method, and the data is JSON formatted.
This is my script which for now is trying to just log it to a file. The file is created and I see the IP address of the request but the output is empty in terms of post data.
ob_start();
echo "Request from :" . $_SERVER[REMOTE_ADDR];
echo "print_r:".print_r($_POST,true);
if(array_key_exists('app_id', $_POST))// me attempting to access a specific key they claim is in the post data
{
echo "app id = " . $_POST['app_id'];
}
//I also tried both of these and neither output anything
//foreach ($_POST as $key => $value) //idea 1
foreach($_POST as $item) //idea 2
{
//echo "key=".$key." value=".$value; //idea 1 log
echo "next=";//idea 2 log
echo $item;
}
$contents = ob_get_flush();
file_put_contents("log.txt",$contents,FILE_APPEND);
There's not a lot to go on here -- who knows what the client is actually sending -- but here's a thought:
POST is just an HTTP command. It's traditional for the body of a POST to be a series of key-value pairs from a form, but it is not actually necessary. It's possible that the remote client is issuing a POST request to your server and then just delivering a JSON blob in the request body, which would not be successfully parsed into the $_POST array.
I recommend exploring the answer at How to get body of a POST in php? to see if that helps shed light on this problem.

how can get google search result

How can I get the result of search in Google search, but without using the API.
I've tried many ways, but Google detects the ip server and prints an error.
The function I use:
fopen
fsocket
...
For example this function can get html from other web sites, but for Google search, Google returns an error.
Is there a way I can get search results from Google?
function openpage($ur)
{
$buf="";
$ur=str_replace(" ","+",$ur);
$fp = fopen($ur, "rb");
if ($fp)
{
while(!feof($fp)) $buf=$buf.fgets($fp);
fclose($fp);
}
return $buf;
}
I have done a small project about it before. I used HTMLagilitypack.
Here is the source you can check it.
https://github.com/serdarb/Google-Results
https://github.com/serdarb/Google-Results/blob/master/frmMain.cs
HtmlAgilityPack.HtmlNodeCollection hrefList = htmlDoc.DocumentNode.SelectNodes("//div//ol//li[#class='g']//h3//a");
But do not send to many requests... Google asks for a captcha or returns error.

Categories