Reply back to a response - php

I need to reply back to a response received.
How should I reply using the response?
I want to reply to a response received in my MDM System. The device contacts the server and delivers status as idle. Now I need to send a xml command directly to the device.

You can't push from PHP Webserver to HTTPClient, the HTTPClient must contact you first (Unless you write a sockets module, then php could be the wrong language) - Only said it as I'm not certain what your trying to achieve.
Anyway straight to the point - Once the device contacts the server, and gives the status 'idle', you can just immediately write out an echo response
if(isset($_POST['status']) && $_POST['status'] == 'idle') {
echo '<?xml version="1.0" encoding="ISO-8859-1"?><root>
<data>blablabla</data>
</root>';
}
etc. Depends on how the devices actually contacts the server, is it sending a post command? Is it actually connecting as a HTTPClient or just a direct TCP/IP? Is there anymore info you can provide?

Related

Retrieving e-mail attachments from outlook.com mailbox via EWS using PHP

I am developing an outlook add-in which is supposed to sync an email to another server for our use case. I was able to get the mail stored on the remote DB but the problem is with fetching attachments. Since an add-in can't itself send an attachment, I have to send the remote server some details such as (ews url, access token, attachment id etc) so that it can use that information the contact the outlook server for getting respective attachments. No matter how I tried it appears to be failing. This basically leaves me thinking does outlook.com supports EWS? Proper references are hard to find but some of those references say 'No'. But my belief is since outlook.com looks like a part of 'Exchange Online' (although free) then it should support EWS too. This is evident by the fact that I am able to retrieve an ewsUrl and an attachmentToken by executing getCallbackTokenAsync() method of the Office.context.mailbox object via my outlook add-in. The add-in documentation says:
"The Office.context.mailbox object provides the getCallbackTokenAsync function to get a token that the remote server can use to authenticate with the Exchange server."
So there it is. But when I try to execute a SOAP request against the ewsUrl with an XML payload (from my remote server), the outlook server endpoint (https://outlook.office365.com/EWS/Exchange.asmx) keeps returning an error 500 - internal server error. There's not much conclusive details as well in the response so I can track down if there's anything wrong with the request I send to it. I am really puzzled at this. Appreciate some help from somebody who has done this already. Below is the code block (Laravel) I execute for your reference:
// check for attachments
if(is_object($incoming->attachments))
{
// we have attachments
$attachments = $incoming->attachments->attachments;
$ewsToken = $incoming->attachments->attachmentToken;
$ewsUrl = $incoming->attachments->ewsUrl;
foreach($attachments as $k => $v){
Log::info('Attachment ID: '.$v->id);
$attachmentSoapRequest = <<<EOD
#"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""https://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""https://www.w3.org/2001/XMLSchema""
xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""
xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">
<soap:Header>
<t:RequestServerVersion Version=""Exchange2013"" />
</soap:Header>
<soap:Body>
<GetAttachment xmlns=""http://schemas.microsoft.com/exchange/services/2006/messages""
xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">
<AttachmentShape/>
<AttachmentIds>
<t:AttachmentId Id=""$v->id""/>
</AttachmentIds>
</GetAttachment>
</soap:Body>
</soap:Envelope>"
EOD;
try{
$response = Http::withBody($attachmentSoapRequest, 'text/xml')->withToken($ewsToken)->withOptions([
'debug' => true,
'allow_redirects' => false
])->post($ewsUrl);
// process response
if ($response->successful()) {
Log::info('received ews response!');
$responseEnvelope = $response->body();
Log::info($responseEnvelope);
} else {
$response->throw();
}
} catch (RequestException $e) {
Log::info('ews request error!');
Log::info($e->getMessage());
}
Log::info(print_r($response, true));
}
}
If somebody knows the answer, it doesn't has to be Laravel specific. Even a pure PHP solution is welcome.
Thanks.

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

connect to a server with PHP and see if that server can ping another

I personally don't see how this can be done even though these are my servers. But I want to know if my servers can reach external sites--ping a generic website for example--have outgoing communication. That is, I want to use execute a PHP script on one server, connecting to another of my servers, and test if the second server can ping a website, for example. So I know how to use PHP on the server my script is executing from to ping a website with fopensocket. I just don't know how to set this up to test another server's pingability. I mean I have the credentials but the only way is to have my script on each and every server and then reach the script and execute them. That is not what I want. I want to do this from the one/external server and just feed my script the ip/port/uid/pwd of the server I want to test.
An easy API would look something like:
SERVER1:
// get response from server2
$response = file_get_contents('http://www.server2.com/api.php?method=ping&ip=IP&port=PORT&uid=UID&pwd=PWD');
// do json_decode() if response is json string
SERVER2 (api.php):
// respond to API call
if (isset($_GET['method']) && $_GET['method'] == 'ping') {
// get other params and do your ping function
echo $pingresult; // perhaps a json encoded array
exit;
}
There is no security so you could send an API password or do it with OAuth or HMAC

Running php code chunk in background

I have a php script which is responsible for reading some request parameters from my iPhone app. Once I do some manipulations to it I save them in db and will need to send some push notification message using apple APNS. So currently its done like this in the code.
<?php
$param1 = $_POST['param1'];
$param2 = $_POST['param2'];
//saving part here
//push notifications
$pushService = new PushService();
$pushService -> init();
$pushService -> push($param1, $param2);
//json response
echo json_encode(array($success, $dbsavedid);
?>
Problem occurs with the push part. Now it takes lot of time for this push notification code chunk to execute because the table has grown with lot of data. Hence the iPhone app waits too long for this to execute (to get the success response to iPhone).
Hence is there any way to make this push part asynchronous and send a response to iPhone side using the echo other than using a separate script for push notifications? Also note that I need to get some data from saved records as well to iPhone side. So I will need the output to reach the iPhone side.
You can force PHP to send a response by using the flush() function for example. (there might be other possibilities to accomplish too)
So what you have to do is write with echo to the output buffer when your db operations finished (these should be really fast if you have 100-1000 records) and right after call the flush() function. Your client should get a response right away.
Also see this link about flush() itself, because there might be other parameters of your enviroment which prevents your response in reaching the client side as soon as expected.
http://php.net/manual/en/function.flush.php
<?php
$param1 = $_POST['param1'];
$param2 = $_POST['param2'];
//saving part here
//json response
echo json_encode(array($success, $dbsavedid);
//response should be sent right away, no need for wait on the pushservice operations
flush();
//push notifications
$pushService = new PushService();
$pushService -> init();
$pushService -> push($param1, $param2);
?>
The actual reasons for this is, my server provider has blocked port 2195 and port 2196 which is used by apple APNS. I believe once you allow it this will be fixed and should work like earlier.

PHP web service, send response before end of script execution

I have a web service written in PHP to which an iPhone app connects to. When the app calls the service, a series of notification messages are sent to Apple's APNs server so it can then send Push Notifications to other users of the app.
This process can be time consuming in some cases and my app has to wait a long time before getting a response. The response is totally independent of the result of the notification messages being sent to the APNs server.
Therefore, I would like the web service to send the response back to the app regardless of whether the messages to APNs have been sent.
I tried using pcntl_fork to solve the problem:
<?php
...
$pid = pcntl_fork();
if($pid == -1)
{
// Could not fork (send response anyway)
echo "response";
}
else if($pid)
{
// Parent process - send response to app
echo "response";
}
else
{
// Child process - send messages to APNs then die
sendMessageAPNs($token_array);
die();
}
?> // end of script
Unfortunately, the parent process seems to wait for the child process to end before sending the response even though I do not use pcntl_wait in the parent process. Am I doing something wrong or is this normal behaviour? If this is normal then is there another way I can solve this problem?
Thank you!
If you're hosting the PHP process in Apache then you really shouldn't use this: see this for the section that says *Process Control should not be enabled within a web server environment and unexpected results may happen if any Process Control functions are used within a web server environment. *.
You should probably set up a separate daemon in your preferred language of choice and hand the APNS communication tasks off to that. If you really really really must try using ob_flush().
I think you can send the response back before doing the "long" process. Take a look at the flush() function of PHP it'll maybe help

Categories