PHP: fsockopen() – Filter header before processing

When making a request with fsockopen the response you receive may not be exactly what you expected, let’s say you make a request to a simple HTML page which has one world which simply says “hi”, what you would expect from the fsockopen response data was the word “hi”, this is true we do receive the word but above that is around 6 lines of extra data. This extra data is called header data and is added to any request or response automatically, when you sent your initial request you would have provided some header data which the web server would use to determine exactly what you’re requesting and the format of the data so it understands the request. In return the web server also returns your data but also includes header data related to the response, this header data is normally picked up via are web browser and processed so we never normally see it.

The way a header request is ended is special as it always ends with a double line break which looks like the following.

Due to this it is easy to filter out the header data using the following line of code.

Simply replace $response_data with whatever variable / data needs the header removed. Here is a link to the following code in a ready to use function.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.