PHP web service for command line users - php

I have been given a task to write a web service to push data from sensors to the server database.
I have written a PHP webservice which takes some arguments in GET, process the data in it and stores the data in the database.
Example
http://www.example.com/pushDataService/index.php?arg1=<Data 1>&arg2=<Data 2>&arg3=<Data 3>
But our firmware programmer wants something that he can test on a command line.
Just like SMTP communication using telnet
Example
Telnet mail.example.com 25
EHLO mail.example.com
AUTH LOGIN
UserName encrypted in base 64
Password encrypted in base 64
MAIL FROM:<test#example.com>
RCPT TO:<test#domain.com>
DATA this is a test message.
Can I make the webservice work like this where he can do the handshaking process in command line and then each argument is sent individually and user get a response for each argument ?
Please help, I am new to PHP and have no idea of (web) service like this!
Thank you
Edit:
We are already using MQTT to push the data to the server but we need another server to push data which will work as a combination of SMTP mail server and MQTT protocol...
Will a SOAP web service do the job for me?

The http://www.example.com/pushDataService/index.php?arg1=<Data 1>&arg2=<Data 2>&arg3=<Data 3> request can be done Just like SMTP communication using telnet as following:
Telnet www.example.com 80
GET /pushDataService/index.php?arg1=<Data 1>&arg2=<Data 2>&arg3=<Data 3> HTTP/1.1
Host: www.example.com
<CR>
<CR>
Pushing data with GET requests is not the best idea. PUT or POST requests would be more suitable. First of all it is not idempotent and should never be cached, and what is more important - it is way more flexible in data format and size. The POST request sending json data may look like
Telnet www.example.com 80
POST /pushDataService/index.php HTTP/1.1
Host: www.example.com
Content-Type: application/json; charset=utf-8
Content-Length: 57
{"arg1":"<Data 1>", "arg2":"<Data 2>", "arg3":"<Data 3>"}

Related

How to send a manual POST request of which I have a file with data?

we're using a custom built cart system and during high loads our payment system (Worldpay) times out.
When this happen we receive an email containing the POST request that failed, and this is a .txt file done like the following:
POST /index.php?xxx=yyy&zzz=xxx HTTP/1.0
Content-Length: 917
Host: ourdomain.com
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
User-Agent: WJHRO/1.0 (WorldPay Java HTTP Request Object)
parameter1=value&parameter2=value2
How can I actually quickly resend this request to my server so it can register the payment now that the load is more normal? I have this in a .txt file, is there a quick way to do it using this file as it is? Curl? Browsers? In a way so I can see the response to check that all is ok.
Thanks so much!
Depending on the needs you have exposed, it is likely that tools such as POSTMAN could help you, allow you to send http requests through an intuitive interface, you can use the various parameters, that are sent to you in the file you mentioned and run the same request check for any errors.

Post request missing payload

I'm building an api that receives incoming webhooks from external parties. Post requests to my application lack a body in some cases. In my logs I see the incoming request with the following header:
Accept: */*
Content-Length:
Content-Type: application/json
As you can see the content-length is empty.
I cannot reproduce the problem. What I've tried thus far:
The request payload is only missing coming from a specific third party. If however, I provide this third party with a different callback url like request bin, the payload is not missing.
Connected this party source to our test environment. Which has exactly the same configuration (checked the entire php.ini) and the same version of our software. On our test server, requests are received with payload.
When sending post requests with Postman to our production environment, webhooks are received with payload.
Both test and production are https. I've tried sending http requests to our production server to see what happens, and I get an error as expected and no received headers in our logs.
Checked the php post_max_size, which is on 24M.
When creating a callback.php file in my public folder, and have the third party send it's webhooks to this destination, I am able to write the results to a log with the following code, which includes a payload. If I to output php://input later on in my Laravel application, it doesn't work:
$postdata = file_get_contents("php://input");
$file = fopen("webhook.log","w");
echo fwrite($file,$postdata);
fclose($file);
Both servers are running on the same php version (php7), and I am at a loss as to what to try next.
Found it! The problem was a port 80 to 443 redirect in a vhost configuration. I earlier dismissed redirects - which seem to be a common source of missing payloads in post requests - as possible cause; I had made a test script which succesfully received payloads on this very same server.
However, I had placed this test script in the servers public folder, which was not subject to the same redirects as was the root of my application. After removing the redirect, payloads where received as expected.

PHP Close Header

I have an app which connects to my web server and transfers data via XML.
The headers I connect with are:
POST /app/API/Data/Receiver.php HTTP/1.1
User-Agent: Custom User Agent 1.0.0
Accept: text/xml
Content-Type: text/xml
Content-Length: 1580
Host: servername.com
The app then handles the data and returns its own XML formatted reply. One of the header's I'm setting in the response is:
header("Connection: close");
When I send connect and send my data from a simple app on my PC (C++) it works fine, I get the close header correctly and the connection is closed as soon as the data is available. When I send the exact same data using a GSM modem and and embedded app, the connection header comes back as:
header("Connection: keep-alive");
The GSM modem also sits and waits until the connection is closed before moving on and often just times out.
Is there someway to close the connection on the server so that the GSM side does not time out?
It is possible that your GSM service provider transparently proxing connections. Try to send data on non-standard port (i.e not 80, 8080, 443)
Also setting cache control header private might work.
Cache-Control: PRIVATE
Headers are just plain text but cannot be sent once data has been sent in PHP. Try this:
echo "\r\n\r\nConnection: close";
die();
and adjust to your needs

how can I send POST requests to some web page with manipulation of the current page?

I want to set headers like post variable name and the value and send and expect response.
this is also a security question, assume I want to send post a form variables of which are action="delete" and userid=100, and lets say, I have found a file which accepts ajax requests.
curl is your friend! :)
Say you've noticed an endpoint at example.org/process.php that a form is posting to. You can tailor you own custom request easily from the command line using curl.
$ curl -X POST --data "action=delete&userid=100" example.org/process.php
The --data or -D flag lets you pass arbitrary POST data just as an HTML form would. You can also set HTTP request headers with equal ease:
$ curl --header "User-Agent: Mosaic" example.org/process.php
You can see exactly what's happening with the -v (for verbose) flag. For the first example above the output is:
* About to connect() to example.org port 80 (#0)
* Trying 192.0.43.10... connected
* Connected to example.org (192.0.43.10) port 80 (#0)
> POST /process.php HTTP/1.1
> User-Agent: curl/7.21.6 (x86_64-apple-darwin10.5.0) libcurl/7.21.6 OpenSSL/1.0.0d zlib/1.2.5 libidn/1.22
> Host: example.org
> Accept: */*
> Content-Length: 24
> Content-Type: application/x-www-form-urlencoded
>
* HTTP 1.0, assume close after body
< HTTP/1.0 302 Found
< Location: http://www.iana.org/domains/example/
< Server: BigIP
* HTTP/1.0 connection set to keep alive!
< Connection: Keep-Alive
< Content-Length: 0
<
* Connection #0 to host example.org left intact
* Closing connection #0
If you're using a *NIX operating system including Mac OS X, you probably already have curl, just open a shell. If you work with Ruby at all, I highly recommend curb, a set of bindings for that language. Most PHP installations come with curl support, although the interface is pretty horrible. The docs are over at php.net.
You can use CURL library for this. Check about more info
You can send data POST/GET method, upload file, SSL support, cookie support, ftp support and much more
You might also want to take a look at Snoopy (http://sourceforge.net/projects/snoopy/)
Its a PHP class designed to act as a web browser with lots of useful functionality like simulating HTTP requests, manipulating form data etc.

twilio bhrowser to browser call issue?

I am following http://www.twilio.com/docs/quickstart/client/browser-to-browser-calls for making a call from browser to browse.
According to documentation open two browser with:
http://127.0.0.1/client.php?client=test1 and 127.0.0.1/client.php?client=test2
When I am using sandbox APP ID:
The both browser couldn't able to connect but it a call connect to the the sandbox number.
When I am using my app ID:
It shows me the following error:
Component: TwiML errors
httpResponse: 502
ErrorCode: 11200
url: http://127.0.0.1/demo.php
Request: What Twilio sent your server
HTTP Method: GET
HTTP URL: http://127.0.0.1/demo.php
HTTP BODY: Key Value
AccountSid xxxxxxxxxxxxxxxxxxxxxxxxx
ApplicationSid dddddddddddddddddddddddddd
Caller client:jenny
CallStatus ringing
Called
To
PhoneNumber tommy
CallSid cccccccccccccccccccccccccccc
From client:jenny
Direction inbound
ApiVersion 2010-04-01
Response: What your web application responded with to Twilio
HTTP Headers: Key Value
Date Thu, 25 Aug 2011 11:36:22 GMT
Content-Length 137
Connection close
Content-Type text/html
Server TwilioProxy/0.7
with <html><head><title>502 Bad Gateway</title></head>
<body><h1>Bad Gateway</h1>An upstream server returned an invalid response.</body></html>
The VoiceURL assigned to your application must be a publicly-accessible URL so that the Twilio servers can reach it. If it's running only 127.0.0.1 that is only accessible from your local machine and we can't reach it.
You may be interested in something like localtunnel.com or ngrok which will let you expose your local server to the public.

Categories