How make redirection with web service? - php

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

Related

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

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.

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);
}
?>

cannot access ajax data sent on the server

I am following this tutorial to make an events calendar-it utilizes backbone and the fullcalendar jquery plugin.
Backbone is responsible for sending to the server(via ajax) event details(start date,end date,title).Here is an image of what is sent to the server.
It is taken by the network panel(headers tab) of Chrome Dev Tools. I would expect that with the following line of code I would access the title of the event:
$title=$conn->real_escape_string($_POST['title']);
But I cannot, I do not understand why this happens. backbone sends JSON to server via the POST method. What am I missing here?
PHP has a problem with parsing json data, because it expects the posted data to be in a Querystring format (key=value&key1=value1).try using this:
$content = file_get_contents("php://input");
You are sending a JSON dictionary in the request body. Use http_get_request_body in PHP to obtain the full JSON string, then json_decode it.

JSON array as value getting text

I have created API for creating a customer in PHP, the client side is in Android would use a POST request to access this API. For testing I have used chrome postman where I had used post data as :
{"name":"test","marks":["12","13","16"],"age":19}
and it is working fine.
I am getting marks as array itself ["12","13","16"].
But from android client I am getting as text ["12","13","14"].
What may be the possible reason?
POST it as :
{"name":"test","marks":[12,13,16],"age":19}
You are posting it as String inside array ["12","13","16"]
try to use it like
{"name":"test","marks":["12","13","16"],"age":19}

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