Category : "PHP"

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 ...

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 ...

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 ...

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. ...

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 $content = file_get_contents('http://www.google.com/'); 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 ...

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 ...

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 ...

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. function filter_var_domain($domain) { if(stripos($domain, 'http://') === 0) { $domain = substr($domain, ...

Download a file using curl in php

Here is a quick curl snippet for php, that can download a remote file and save it. <?php set_time_limit(0); //File to save the contents to $fp = fopen ('files2.tar', 'w+'); $url = "http://localhost/files.tar"; //Here is the file we are downloading, replace spaces with %20 $ch = curl_init(str_replace(" ","%20",$url)); curl_setopt($ch, CURLOPT_TIMEOUT, 50); //give curl the file pointer so that it can write to it curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $data = curl_exec($ch);//get ...

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 $path = array_search('uri', @array_flip(stream_get_meta_data($GLOBALS=tmpfile()))); file_put_contents($path, 'hello'); echo file_get_contents($path); ...

Pages:123456789»


Connect with us