How do you send back data when you get a webhook? - php

I have a quick question. I am new to webhooks and the service I am using requires a response. I am doing this in php and here are their instructions:
We require you to verify the ownership of the server you are making WebHook calls to by adding the following object parameters to your JSON response.
{ "details" : { "zippy_token":"xxxxxxxxxxxxxxxx" }}
After adding the JSON response code to your WebHook endpoint, come back here and click the green Add Webhook button. Zippykind will verify your WebHook by making a POST call to your WebHook URL with the handshake within the parameters, afterwhich will add the verified WebHook to your active list of WebHooks. You only need to do this once, after the WebHook has been verified, you can remove the zippy_token parameter from your JSON response.
I see how to get the data but how do I send the info needed (the token) back?
This will be done in PHP.
Thanks

If your site, or what you're developing is located at abc.com, you'd essentially need to create a php script for your webhook callback. abc.com/testwebhook.php.
Inside of the testwebhook.php, you'll output a JSON response with the data formatted as they expect to receive it { "details" : { "zippy_token":"xxxxxxxxxxxxxxxx" }}
In case the service you're interacting with (Zippy) is checking the header output of your response, you may need to set the header via PHP within your testwebhook.php script: header('Content-Type: application/json');
Example - testwebhook.php
`<?php
$output = '{ "details" : { "zippy_token":"xxxxxxxxxxxxxxxx" }}';
header('Content-Type: application/json');
echo $output;
exit;
?>
You'll need to ensure that your json is properly formatted and the endpoint doesn't expect more data returned.
Then the rest is explained ini your initial question. Create the webhook by entering the URL at the Zippy service that gave you those instructions and add the url to the script you setup abc.com/testwebhook.php.
That should be all you need to do.

Related

How make redirection with web service?

I have a web-service which send JSON object as response to log my user.
But if I take the URL of the service and I put it on my navigator I see the json object, how I can send the json througth a redirection ?
Example :
Facebook login -> if we take the action in the form that call facebook/login?...
If facebook use JSON to send a response of the log i must see the JSON object if i call facebook/login?... but I was redirected to the main page, how that works ?
Thank for your reply.
The answer is to check the $_SERVER['HTTP_X_REQUESTED_WITH'] after sending an specific header in the AJAX request.
I have a last question in like :
Can i use a same like this the volley library, to allow the access to a php script only for an android app ?
You can see an example on header in this link :
https://gist.github.com/FabrizioCaldarelli/ad1118e5d8ab0661ba36

How to receive JSON data from a HTTP POST web request using WebHooks IFTTT

basically I want to know how to receive the JSON data from IFTTT when sending a web request to a specific URL on my web server.
I know that IFTTT will send a web request with the JSON data to my .php file which is a public hosted web page (92.123.xxx.xxx:8089/MyPhpFile.php), but how would I know if that web request was actually sent or not because when I try running my PHP script on my web browser it just says nothing ie no POST data was received.
Basic Flow Of IFTTT Setup:
Tell google home (google assistant) to switch TV1 to Xbox.
Run WebHook Applet which sends a web request to the 92.123.xxx.xxx:8089/MyPhpFile.php using method POST, content type application/json and body {“token”:”mseries”,”command”: “{{NumberField}}”, ”test”: “data”}
???? This is where I become confused because I need to Receive JSON data and execute the proper python scripts to send telnet commands to my matrix switcher.
MyPhpFile.php
<?php
$token = "mseries";
# Capture JSON content
$input=json_decode(file_get_contents('php://input'), true);
# Check if correct TOKEN passed or else echo nothing
if($input['token'] != $token) {
echo "nothing";
exit;
}
switch ($input['test']) {
case 'data':
echo print_r($data);
}
?>

php http post response for web hook

I'm trying to create a web hook notification. The documentation of the service i want to use requires that i specify a URL where POST requests can be performed. This URL will receive the following object, in json format, and must respond with a Status Code between 200-299.
{
"type": "ping"
}
I don't know how to proceed making my server on localhost respond with a 200 status code. http_response_code(200) works well on live server but nothing seem to be happening on localhost.
Is there any way i can make it work with localhost?
I've included the link to the documentation here (i hope it's not against the rule).
I am thinking that you wouldn't have to send them the response. The webhook would know about the response. If it reached your URL successfully, it would be a 200 OK right off the bat. If the API is requesting a response back then I imagine that you would have to call it back somehow. Is this a well-known API? Any documentation?
The response code is in the response header, not in the content.
PHP defaults to a response code of 200, so if you don't mess with it at all, you should be good.
If you want to set a different response code (202 for example), just call:
http_response_code(202);
Or set the full header yourself:
header('HTTP/1.1 202 Accepted');
Proper way to explicitly set 200 (or any other) status code with http_response_code function is just as following (don't echo or json_encode it):
http_response_code(200);
It should force webserver to use 200 status code in it's response. However, webserver could possibly ignore it. To check what response code your webserver sends, use telnet or any REST tool like Postman

Slim v3 how to send a response to a different URL / Ip address?

i try to implement the paypal IPN with slim.
see code here
they use curl to make a post request to an URL. Is it not possible to just change the URL in the slim response object?
other ways to send requests to somewhere with slim?
EDIT
I am talking about this line:
$res = curl_exec($ch);
what is the equivalent slim way to send a request to some url?
You can send PSR7 Requests with the help of HTTPlug.
You have to create some Adapter classes to tell HTTPlug how to create Slims Request and Response objects. In your code you just create your Slim Request and call the Client with this. You can choose between some clients, for example Curl or Socket.
If you want to send your user to PayPal in order for them to pay, then you need to create a form on your website which POSTs to PayPal directly.
The code you linked to is a POST request from PayPal back to your server. To handle that in Slim, you create a post route:
$app->post('/paypal-ipn', PayPalIpnAction::class);
Within your Action class you need to send POST request back to PayPal:
class PayPalIpnAction
{
public function __invoke($request, $response, $args)
{
$dataFromPaypal = $reqest->getParsedBody();
// Validate data from PayPal using HTTPlug, Guzzle or
// you use can the PayPal example code directly.
// If data is valid, process data and do whatever you
// need to with it.
// All done. Return the response.
return $response;
}
}
Note that the verification step back to PayPal isn't directly related to the handling of the notification in Slim as it's part of your code for handling the notification.

How to GET remote JSON or XML API data from within PHP and assign a return object as PHP variable?

What I'm doing:
I'm writing a custom program in PHP which pulls data via API from an online LMS service. Right now, I'm trying to implement the available single-sign-on functionality.
This part of the program needs to execute a GET request to the API when a button is clicked (via js or php POST or ?) and ultimately redirect the users browser to a URL which is supplied in the response from the API.
The API allows the choice of an XML or JSON response and I would prefer to use JSON but will make do with XML if needed.
From the API documentation on making requests:
All requests listed in this document should contain a content-type
(XML or JSON) in the request header and be prefixed with the following
base Uri: https://api.example.com/v1.svc
E.g. The Uri to GET a list of Users in XML format would be:
Content-Type: text/xml
GET https://api.example.com/v1.svc/users?apikey=MY-KEY&source=MY-APP
Below is what I'm trying to implement:
How to get the a user's LoginKey
Once you have the user id that you want to sign on you need to make a
GET request to /users/{user-id} which will return information about
the user. Included in this is a LoginKey which you can use to redirect
the user's browser to.
eg.
GET
https://api.example.com/v1.svc/users/USER-ID?apikey=YOUR_API_KEY&source=sampleapp
Response from API:
<User>
<Id>abc12345678</Id>
<UserName>rich_demo#example.com</UserName>
<FirstName>Rich</FirstName>
<LastName>Chetwynd</LastName>
.....
<LoginKey>https://demo.example.com/login.aspx?loginkey=xxxzzzyyy777222</LoginKey>
</User>
The <LoginKey> object data is the URL which I need to ultimately redirect the user's browser to.
I am new to working with APIs and have tried a ton of methods which I could not get to work before posting. If you know how to accomplish this I would be very grateful if you shared your knowledge.
Thanks.
From a HTML <form>, use a traditional post (not AJAX) to a PHP script that does this:
if(isset($_POST['userid']))
{
$userId = (int)$_POST['userid'];
$obj = simplexml_load_file('https://api.xxx.com/v1.svc/users/' . $userId . '?apikey=YOUR_API_KEY&source=sampleapp');
if($obj && isset($obj->LoginKey))
{
$loginKey = $obj->LoginKey;
header('Location: ' . $loginKey);
}
else
{
// failed to load the xml
}
}
If you want to do it with JSON you can use file_get_contents() to get the raw JSON from a URL, then use json_decode() to turn it into an object.
Also, if you want to do it via AJAX, you will have to echo the URL with PHP instead of trying to redirect, then have Javascript do the redirect with window.location.href = '...'

Categories