I received a get request from a site to my site. I can able to know what kind of request by using $_SERVER['REQUEST_METHOD']. Its returning GET request. Now, i need to know how to know what is the request and also generate response for the request in php.
Thanks in advance.
For reading the request do the following:
$webhook = fopen('php://input' , 'rb');
while (!feof($webhook)) {
$webhookContent .= fread($webhook, 4096);
}
fclose($webhook);
Then to respond you can simply echo back like:
echo "I've received your request"
Or you can respond with empty headers. Forexample PayPal doesn't require you to respond with anything just send back the 200 header like:
header('HTTP/1.1 200 OK');
If the received data is in Json format then you can process it's contents by doing something like:
$json = json_decode($webhookContent, true);
If that request has a key like webhookname then you can get it by doing:
$key = $json['webhookname'];
Hope I've covered everything.
Related
About
I am trying to receive message posted on my server as soon as user post message the message in group or channel or direct in slack.
App Status
Code in the verified file where challenge was posted.
header('Content-type: application/json');
$myfile = fopen("test.txt", "w") or die("Unable to open file!");
$data = json_decode(file_get_contents('php://input'), true);
fwrite($myfile, $data["challenge"]);
fclose($myfile);
$json = '{"challenge":' . $data["challenge"] . '}';
echo json_encode(["challenge" => $json]);
Question
Now that the above url has been verified successfully, I am still not able to receive the posted messages. I was expecting messages posted at same url which was used to verify challenge parameter. Is that correct?
Am I missing anything retrieving the messages posted on my server?
Update - 1
Due to some reasons I am not even able to verify the url anymore. My server is not receiving any data. I am trying to save whatever is being posted my side but it is always blank everytime,
I think your assumptions are correct.
According to slack documentation you should be receiving posts with the given payload in this endpoint:
https://api.slack.com/apis/connections/events-api#the-events-api__receiving-events__events-dispatched-as-json
I'm still unsure how are you validating that this isn't the case though, if you kept the code above you would see no result of the post data sent by slack, also if you don't return a response status 200, slack will stop posting to this endpoint after 1 hour.
https://api.slack.com/apis/connections/events-api#the-events-api__responding-to-events
Can you try adding test.php file something like:
<?php
// get all the POST requests
if ($_SERVER["REQUEST_METHOD"] == "POST") {
file_put_contents('test.json', json_encode($_POST, JSON_PRETTY_PRINT), FILE_APPEND);
// return 200
http_response_code(200);
echo json_encode(["success" => true]);
return;
}
At the very beginning, validate the url again and check if the requests start being saved on test.json.
Alternatively can you check if you web server is routing POST requests to this url?
Try using postman to validate that
I tried implementing the google calendar push notification in my server.
Using the outhplayground, i was able to successfully subscribe to the service.
I am getting notifications to my registered url when a change takes place in the calendar.
The only issue is that the response that i receive doesnt have data. Its an empty response.
Could anyone tell me what the issue would be. I am using PHP code in the backend to access the request hitting my url.
authplayground code:
POST /calendar/v3/calendars/calendarname#gmail.com/events/watch HTTP/1.1
Host: www.googleapis.com
Content-length: 161
Content-type: application/json
Authorization: Bearer access_token
{
"id": "01234267-89a6-cdef-0123456789ab", // Your channel ID.
"type": "web_hook",
"address": "https://example.com/response" // Your receiving URL.
}
Code to accept request:
$json = file_get_contents('php://input');
$request = json_decode($json, true);
$post_request = $_POST;
$get_request = $_REQUEST;
As I was getting an empty response, i tried writing the code to accept any possible way.
Google sends the response in the headers as an array.
Try this:
$response = apache_request_headers();
if($response) {
print_r($response);
}else {
echo 'The apache_request_headers() did not find any headers.'; //Or google is not sending any.
}
You may also try:
getallheaders()
if the apache_request_headers did not work.
Testing this can be difficult. You may want to set up a log that sends any data your page gets to a table on your database so that you can go back and inspect to see what type of progress you are making.
You can get the values like this:
$channelId = $_SERVER['HTTP_X_GOOG_CHANNEL_ID'];
$resourceId = $_SERVER['HTTP_X_GOOG_RESOURCE_ID'];
if($channelId) {
file_put_contents('webhook.txt', $channelId.' ||| '.$resourceId);
}
You can get the different headers here: https://developers.google.com/calendar/v3/push#understanding-the-notification-message-format
I am sending & receiving JSON responses from a third party API, in one instance though they need to send a response to a url on my site ie: https://my-wordpress-site/api/confirm?transaction=T0efewgfweg78105 Can someone point me in the right direction on how to receive the payload from this response? The payload will be a JSON response like: “Status”: "Success" What script will I run on https://my-wordpress-site/api/confirm.php
If I understand the situation correctly
You can fetch this data with this snippet:
if(isset($_REQUEST['transaction']) && !empty($_REQUEST['transaction']))
{
$response_body = file_get_contents('php://input');
$data = json_decode($response_body);
}
php://input is a read-only stream that allows you to read raw data from the request body
One of my partners has an API service which should send an HTTP POST request whenever a new file is published. This requires me to have an api file which will get the POST this way:
http://myip:port/api/ReciveFile
and requesting that the JSON format request should be:
{
"FILE ":"filename.zip",
"FILE_ID":"123",
"FILE_DESC":"PRUPOUS_FILE",
"EXTRAVAR":"",
"EXTRAVAR2":"",
"USERID":" xxxxxxxxxxxx",
"PASSWORD":"yyyyyyyyyyy"
}
Meanwhile it should issue a response, in JSON format if it got the file or not
{"RESULT_CODE":0,"RESULT_DESCR":""}
{"RESULT_CODE":1001,"RESULT_DESCR":"Bad request"}
And after, when I am finished elaborating the file, I should send back the modified file same way.
The question is, now basically from what I understand he will send me the variables witch I have to read, download the file, and send a response back.
I am not really sure how to do this any sample code would be welcomed!
I'm not sure exactly what the question is, but if you mean creating a success response in JSON for if an action occurred while adding data to it which is what can be understood from the question, just create an array with the values you wish to send back to the provider and do json_encode on the array which should create json and just print it back as a response.
About receiving the information; all you have to do is use the integrated curl functions or use a wrapper (Guzzle, etc) to output the raw JSON or json_decode data into a variable and do whatever you please with it.
From what I read in the question, you wish to modify the contents of it. This can be achieved by just decoding the json and changing the variables in the array, then printing the modified JSON back as a response.
Example (this uses GuzzleHTTP as an example):
$res = $client->request('GET', 'url');
$json = $res->getBody();
$array = json_decode($json, 1); // decode the json
// Start modifying the values or adding
$array['value_to_modify'] = $whatever;
$filename = $array['filename']; // get filename
// make a new request
$res = $client->request('GET', 'url/'.$filename);
// get the body of specified filename
$body = $res->getBody();
// Return the array.
echo json_encode($body);
References:
http://docs.guzzlephp.org/en/latest/
I am trying to build a post server similar to posttestserver.com and have been runnning into lots of trouble.
The following returns nothing -
do {
$data = file_get_contents('php://input');
} while (empty($data));
header('HTTP/1.0 200 OK');
header('Content-Type: text/html');
var_dump($data);
I have also had a look into the use of sockets but the client should be directed to a URL rather than an ip/port for the clients ease. I suspect that this is what i need to use but am not sure where to start.
For what its worth, the client expects an HTTP 2XX response code from its HTTP POST request, and the client will not attempt submitting the next HTTP POST request while a previous request is still in flight.
Any ideas?
It would seem that you cannot capture and view the POST data in the one browser window.
For what its worth, here is the code that worked in the end -
$data = file_get_contents('php://input');
//do something with the data such as write to file or database
Then you could use the data in another PHP script.