How does a webserver interface with PHP - php

I want to handle http requests via another webserver or own written server in future.
I want to understand how to provide php with request data properly.
in what form request data should be provided
how data is provided to php, via stdin or somehow else
how php handles received request data afterwards, any additional actions required to fill $_SERVER variables etc.

It's pretty simple actually. The Webserver communicates with PHP through the CGI interface. This entails setting up environment variables, invoking the php interpreter, piping a POST body through stdin, and then reading the PHP response from stdout.
Call PHP from virtual/custom "web server"
How to pass POST data to the PHP-CGI?
http://pear.php.net/package/HTTP_Server
What is Common Gateway Interface (CGI)?
As to PHP postprocessing the $_SERVER variables: That's fairly minimal, it only constructs PHP_SELF and PHP_AUTH_USER etc. as documented in the manual. The rest is provided by the webserver (e.g. all HTTP headers converted into HTTP_* env variables).

Go download the source code for php, and look at the code for mod_php, written in C I believe, cause that's where it's done.

Related

IIS with PHP: How to get the data from a SOAP envelope?

An external application is calling my PHP script and passing URL parameters and a SOAP envelope. I can access the URL parameters in the $_GET array. However I cannot access the SOAP envelope. I was expecting it in the $_POST array, but that is empty.
The IIS log clearly shows that the SOAP envelope is being received by IIS but somehow I cannot access it in PHP - or I am using the wrong methods/variables.
When I check the "failed requests log" in IIS, I see the SOAP data as "GENERAL_REQUEST_ENTITY".
However I am at a loss when trying to access said data from within my PHP script. Is this kind of content stored in a different variable (other than $_GET, $_POST or $_REQUEST)? Or is IIS not forwarding this to my script and if so, why not?
Edit: I am not tied to PHP if PHP is not capable of doing this, I am open to any language that gets the job done. However my knowledge of ASP.NET is more limited than that of PHP. Is there a better language for capturing the missing data from the request?
This problem is not going away and I need to find a solution. So far no matter how much I search via Google, I keep running into dead ends.
Edit #2: Here is a screenshot of the log file for clarification. It clearly shows the XML and so does Fiddler, which I have also just now tested:
How can I capture this XML with PHP (or ASP.NET for that matter)?
Best regards
Max

How do I set up my server to work with PHP

I want to use PHP with a server that i've coded my self. (Not Apache etc) I guess I have to send the http request and some additional data from my server to php, but I dont know how to make that connection and how to format that message.
I know I can run scripts with php.exe, the problem is that way it won't work with php sessions.
If you Google for the CGI specification, this will give you a reasonable idea of what environment variables to set up. Usually you exec the php process with the script filename as an argument, supplying it with the new environment variables. Are you familiar with fork, exec* system calls?
It's a little more complicated than that, since it involves fiddling with standard input, output and error streams. To give you an idea, check out the thttpd source code, in the file libhttpd.c, function cgi_child.
If your server source code is not in C or C++, you will have to dig around for spawning a child process to handle the PHP script being called in your server, and send the output to the browser. Some of that output will be HTTP headers, and it may stop executing after that (e.g. HTTP 2xx no content, HTTP 3xx redirect) in which case you send that back to the browser with \r\n\r\n to terminate the output.
To send the information to the PHP process, just set up the environment and possibly argument variables unless the process reads the environment variable SCRIPT_FILENAME you set up (see CGI environment variables). Change into the directory where the binary is or into the document root, whichever makes sense, and before spawning, handle incoming data (e.g. from POST request) and let the spawned process read it from stdin (from php-fpm, it probably reads from the socket you set up, but not entirely sure, so that's left as an exerciese).
It might be easier to run or spawn php-cgi which is php-fpm, which listens on a default or specified address and port. Run php-fpm -h for options and give it a whirl. This is definitely the way to go for session support I think. Also make sure the process knows where to look for the php.ini file.
As well as the CGI spec, it would be helpful to read all about HTTP. Or at least a good chunk of it.

How does PHP set HTTP headers with the header() function?

I am learning HTTP and web programming and I was surprised to find out that you can set HTTP headers using php. I thought PHP was used to generate dynamic HTML web pages and create applications. It seems strange to me that PHP can set headers that are sent by the web server (e.g. Apache). I know PHP interpreter reads the PHP file and generates an output usually in the form of HTML.
Does this process work with pipes? In my opinion the apache server has to be able to receive commands from the PHP interpreter or it has to be able to interpret PHP functions itself. They are separate processes I think.
What mechanism is used by PHP to set headers to the web server application (httpd or Apache or something else)
Do all web servers support receiving and setting headers that are received via PHP?
Is it possible to set HTTP headers with all backend languages?
I searched through the website and I did not find an aswer to my question.
More specifically I want to know what command can apache or other web server send to PHP.exe application or PHP-CGI.exe application to receive other information besides the outputted HTML file.
Indeed an interesting question. Using command-line tools, I can only access the produced HTML output. Hence php -r"header('Location: http://someurl.com');" will produce nothing from command line.
When I would look at my setup with IIS (not Apache) though, I see that IIS is using PHP-CGI.exe to communicate with PHP. Looking at the optional arguments of PHP-CGI.exe, I see -b can be used to set a Bind Path for external FASTCGI Server mode. I guess in this server mode there will be room to communicate header information separate from produced HTML.
I don't know the exact details of the FASTCGI protocols to go more indepth. But I guess this is what you wanted to know.
EDIT:
When googling about this, I came upon this thread:
How does PHP interface with Apache?
Sending headers from PHP is very useful, for example, you can send headers that force the browser to download a file vs. displaying it on screen (for file scripts, reports that output CSV), controlling cache headers and performing page redirection.
As stated in one of the comments, PHP being a module of Apache in many installs, it sends headers directly through that. Otherwise, the headers would be sent via CGI / FastCGI, or PHP-FPM for nginx.
What you're thinking of more or less is of a templating engine, which PHP performs well at, but PHP has other functions that would normally not be seen in a template engine, such as opening sockets, handling files, and so on.
Any backend language I've had experience with has support for sending HTTP headers, and I would consider any web-oriented back-end language incomplete without this ability.
header('Location: http://www.google.com/');
In this way we can set header in php

Passing variables to another webserver

Is it possible to pass variables from one webserver to another using php? I need to be able to pass variables from a webhost to a local server for processing and I dont know if it can be done.
You have to communicate between the servers somehow. How you do that communication determines if, and how, you would pass data. (You can't generally pass actual variables, just the contents of them).
If, for instance, you were communicating with HTTP then you could pass the data in the query string of the URI, or in the body of a POST request.
Post a web request to that server.
You can do that using curl and add header data or post data too.
Check php curl manual
Simple post request will also do the trick.
There are many ways you could do this. The simplest way and most service oriented would be to make a "catcher" php script which will recieve the variables on the target server and then the local server can run:
file_get_contents('http://targetserver.com/catcher.php?var1=val1&var2=val2&var3=val3...');
also look into sockets and direct connections, FTP, and other ways to do it.

PHP Response directly from C / C++ language

I am looking for an efficient way of responding to an http request. When a http request is received (apache/php webserver environment), I want to know if I can obtain a reference to php's response instance inside a c++ library method and write the output data directly to the response instance within C++ context.
Normally, you'd simply redirect the request to your external C/C++ program through mod_fastcgi, processs it completely in your program and write the repsponse to the client.
If not possible, you could include the program output into the generated web page by accessing your program from Apache as a server side include (SSI).
From PHP, you can also perform a system call to the external program and process the returned output as shown in the link.
Some simple C examples are provided in the fastcgi documentation.
To write a PHP extension in C/C++ (here and there) and make that work in Apache (through mod_php) is possible, but is considered rocket science and therefore discouraged ;-)

Categories