PHP - Receiving XML requests - php

I am sending packets of data to a desktop application using PHP Curl, this works using a specific IP address and port to request and post information.
What I need to do now is the opposite and send information from a desktop application and process them on the web as soon as I receive them.
The information will be sent in XML format.
I've been looking into how I can achieve this and so far I have come across NuSOAP and PHP. I am just wondering if this is the way forward to achieve what it is I need to do.
As much information would be great! Thanks

This is actually a one-liner in php:
$xml = simplexml_load_file('php://input');
This assumes that
php is running behind a web server (typically, Apache)
the request is a standard HTTP POST request
xml data is sent raw in a request body

Related

How web-services handle requests and responses, and how PHP could access this correctly?

I'm having troubles to figure out how web services handle requests and send responses, and how my site/module will handle it. So these are my questions and bellow a little about why i am asking this:
What is the right way to send a request to web service?
How a web-service will usually answer a site requesting?
How a PHP site can send a request to a web-service? Is cURL the right way?
I'm a student who are learning PHP and a lot of other things, and my job now is create a Joomla Module to show information from a web service (probably created in java or something, probably created by me, when i learn java...).
I know i will use http requests to talk with the web service, but i worry im using it wrong (making a request to the url, like the browser).
So, i did a little example site and a example API. The api uses Slim microframework to create routes, access my database and return the content in json (what means: if i access 'api.com/api/something' in my browser i see a plain white page with a lot of json). I pretend this is my web service.
The example site send a request to the API with cURL, reads the content and decode the json and do things. It seems forced to me.
I have not much support to understand web services and if i am in the right way (or far from this).
I would appreciate your help.
You are on the right track. A web service is simply a processing or storage facility intended to be accessed by some other program just like a database or fileserver service.
What is the right way to send a request to a web service
It depends. The most common implementations use SOAP or REST which define additional semantics on top of the HTTP protocol. SOAP uses a single URL as a gateway to the service and more specific selection of the functionality and the relevant data is embedded within an XML payload presented via POST. HTTP is merely a bearer for the message exchange. In REST, the HTTP part is integrated into the semantics of the transaction.
The URL identifies the location of the data (or processing function)
The payload contains only data, usually presented as POSTed JSON or XML,
further REST uses the HTTP verb (GET, POST, PUT, DELETE) to indicate the requested action
the HTTP headers are used to convey transaction meta-data.
How a web service will usually answer a request
I'm not sure what you are asking here. It should report back on the state of the request, any relevant error messages and possibly some data.
The speciifics would be unique to the API and documented.
Is cURL the right way?
While it is possible to do a lot with the HTTP wrappers functionality in PHP, libcurl offers an lot more flexibility. So, yes this it would be hard to implement a REST client without using cURL, OTOH a SOAP client is (usually) less complex at the HTTP tier but life is a lot simpler if you use a SOAP library/framework to abstract the more complex protocol.
For future questions please have one question per entry.
What is the right way to send a request to web service?
That really depends on the web service, what they require. It can be as simple as a short text string, to sending a XML formatted or JSON formatted array. You need to research the relevant web service.
How a web-service will usually answer a site requesting?
Again it depends on the web service. They are not the same. A web service will have documentation on what it expects and how it will respond.
How a PHP site can send a request to a web-service? Is cURL the right way?
Curl is a good method and is usually the method used from within PHP.

Node, React web-server with a PHP api server

When communicating between a Node/React web server and a PHP api server is it best practice to "proxy" all requests by the client (React) through the web server and not directly to the api server?
Im using universal (isomorphic) React rendered on the Node web server. It would also use the api server to build the initial page loads (isomorphic).
That's what I would do. Actually that's what i'm doing (with a java based backend/api). It allows you to hide the real structure of your api and also to eventually parse the result in the node server if needed. Also it allows you to adapt the request method to fit exactly your client services (if for example your api is not exactly sending the data the format and the way you want).
For me it seems to be a good practice (and I agree trying to close this question is crazy, this is a good question ...)

How to capture the actual data that is being transfred between Web Server and WebService

I wrote PHP code that allows me to communicate with a REST API.
My PHP call issues a cURL call to communicate with the API service. However, I am running into a weird issue where the data that is being received by the API is not what is being sent using cURL.
I would like to setup some type of a middle man to ensure that the request that I send to from Apache is the same data that is received by the API.
The information that I am interested to see is
Request/Received Header
Request/Received Body
I am thinking about ruining Wireshark on the Apache server to capture the packets that are going from my Apache to the API and also everything that is being received from the API to Apache.
After installing Wireshark on the Apache server "a Windows 7 machine" I see a lot of data.
How can I tell Wireshark to only capture the data from Apache to the API and from the API to Apache only?
Thank you for your help.
There are many tools for API tsting, starting from very easy to use browser plugins and finishing with someting quite complicated as Apache Jmeter. The idea is to make the calls from your testing software to the API and analyse the request and response. if you are on windows you can use something like Fiddler to intercept the packages that are going out from your machine. If you have access to client server I assume there are tools for various OS to intercept the outgoing packages and analyse them, but you would have to google for those.
to filter HTTP results in Wireshark, just type http in the filter field and click Apply
to have a better look, just start th capture before the HTTP request and stop it juste after
afterward, if you know the IP of the API server, you can filter a step more with :
http && ip.dst == X.X.X.X

Using Delphi XE5 on Android to get HTML response from webserver

Setup on my server is a set of PHP scripts which when run will interface with a MySQL database and return information in the form of a HTML response. I want my android application to be able to 'navigate' to one of these pages and post/get the information required for validation and the query, and then get a response.
I cant find out how to get the HTML response information from a browser or find any other method to do this communication. I cannot connect directly with the SQL database as the php scripts are the interface for an application and website also and I want to connect all platforms through the php scripts.
Does anyone know of a means by which I can communicate with the PHP server and read the response details?
Delphi ships with Indy preinstalled. Indy works on mobile platforms, including Android. You can use Indy's TIdHTTP component to send HTTP GET and POST requests to your webserver as needed. Response data can be given to you as either a String (which TIdHTTP will decode to UTF-16 for you) or a TStream (raw data).

Debugging non-browser HTTP requests in PHP

I'm building a PHP server that responds to http GET/POST requests from mobile applications.
I'm fairly ok with using xdebug to debug http requests that are initiated via my browser etc. Using xdebug if I enter a url in my browser and set an appropriate breakpoint in PHPStorm - there's no problem.
How do you do it for a non-browser request ? In my case I'm writing an iPhone / Android app that collects GPS location data and then POSTs the data via HTTP after JSON and urlencoding to my server for storage/analysis.
Running my app in the XCode simulator or Eclipse for example I can get an idea of what the server thinks it received from my app by echoing back what it received with an HTTPResponse.
What are the alternatives? Do I just have to log the incoming content data to file on my server, or is it possible to set breakpoints using an alternative to xdebug ?
Would appreciate any tips or pointers! Thanks!
By using a debugging proxy such as Charles (http://www.charlesproxy.com/), you can set your mobile device to route requests through the proxy application.
You can then view the request and response headers and body.
Charles also provides various helpful features such as allowing you to edit and repeat requests.

Categories