Php tutorials
Output buffering in php and apache
Output Buffering Output buffering is processing mechanism where the output being generated by a program is held in a place till its size reaches a limit or the generation is complete. Only after, will the output be send to its destination. In php for example, doing and echo generates some output. Now this output might be buffered. If its not, then the output will appear on your browser as soon as it is generated. If [...]
Php – Do not rely on set_time_limit too much
Php set_time_limit Php has a function called set_time_limit which can be used to dynamically adjust the maximum execution time permitted to a script. It allows specifying the time in seconds and limits the script execution time to that many seconds. The set_time_limit function whenever called, effectively extends the script execution time by that many seconds. So if the script has already run for 15 seconds and set_time_limit(30) is called, then it would run for a [...]
How to modify a SoapClient request in php
SOAP is a protocol to exchange objects over http. It is used to implement apis and the data being exchanged is in xml format. Sometimes it might be required to modify the soap request to add custom http headers for example. Since SoapClient is a class, it can be extended and the request process can be modified. Here is a quick example The __doRequest method of the SoapClient class is over-ridden and the request is [...]
Php – Fix “Input is not proper UTF-8, indicate encoding” error when loading xml
When loading xml files in php through simplexml_load_string or domDocument class, sometimes an error like this might popup Warning: DOMDocument::loadXML(): Input is not proper UTF-8, indicate encoding ! OR Warning: simplexml_load_string(): Entity: line 93: parser error : Input is not proper UTF-8, indicate encoding ! The error occurs when the xml has some invalid characters that do not fit in the utf-8 character set. The solution to fix this error is quite simple. Just convert [...]
Php – Fetch gzipped content over http with file_get_contents
The file_get_contents function is often used to quickly fetch a http url or resource. Usage is very simple and appears like this However the file_get_contents does not get the contents compressed. It requests the server to send everything in plain text format. Most websites are capable of serving compressed content, if they are asked to do so in the http headers. Compressing the content saves bandwidth and speeds up the transfer process. So the trick [...]
Socket programming with streams in php
Socket programming involves connecting to remote machines on LAN or over the internet using ip addresses and port number. For example google.com has an ip “173.194.36.3″ and runs http server on port 80. So a socket application can connect to that ip address on that particular port number and start data communication. Socket extension Php comes with a socket extension which provide c-style socket functions and can be used to write socket programs. However the [...]
Parse the user agent with ua-parser in php – detect browser, os and device
In your web application you might need to parse the user’s user-agent string to find out the browser/OS/device being used. This is necessary specially when your webapp tries to adapt to the user’s platform in a better way. For example a different layout for mobile device or restricted features/graceful degradation on a non supporting browser. To do this you can analyse the user agent string in php which is found in the $_SERVER superglobal string. [...]
Validate domain name using filter_var function in php
The filter_var function of php is capable of validating many things like emails, urls, ip addresses etc. It does not have a direct option to validate a domain name however. So I coded up this little snippet that the filter_var function with a little tweak so that it can validate domain names as well. Now use it as Last Updated On : 21st March 2013
Download a file using curl in php
Here is a quick curl snippet for php, that can download a remote file and save it. The CURLOPT_FILE option takes a file resources and writes the content of the url to that file resource/handle. Also set the script time limit to something large so that the script does not end when downloading large files. Last Updated On : 19th March 2013
Get the path of tmpfile in php
tmpfile is a very handy function to create temporary files. It returns the file handle/resource. If the path of the file is needed, for use with file_put_contents for example then here is a quick trick to do that Last Updated On : 14th March 2013