With the release of PHP 5.1.0b3 the dev tree has been closed for new features, allowing only bug fixes to facilitate the stabilization of the code for the upcoming (don't ask me when) 5.1.0 stable. This however created a unfortunate situation where PHP does not have a development tree for feature enchantments, so any improvements remain lingering on developer's boxes until development tree is once again available...
In the past few weeks I've been doing some work, which involved using parse_url() and cURL extension quite a bit and in the process came up with few improvements.
parse_url() tweak
[ patch ]
This patch adds a 2nd optional parameter to parse_url() which allows the function to return a particular URL component rather then an array. Ex:
PHP:
<?php
$host = parse_url("http://www.ilia.ws/gallery/", PHP_URL_HOST);
echo $host; // will print www.ilia.ws
?>
cURL improvements
[ patch ]
The patch does two things; first of all it allows the user to retrieve the request sent by cURL to the server. I found this to be very handy and necessary when trying to analyze the full content of the client to server communication.
PHP:
<?php
$ch = curl_init($url);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1); // track request information
curl_exec($ch);
curl_getinfo($ch, CURLINFO_HEADER_OUT) // retrieve sent request as a string
?>
The second part of the patch introduces the convenience function, curl_setopt_array() that allows setting of multiple configuration directives via a single PHP function call. The array of option is based on option name, numeric CURLINFO key and its value.
PHP:
<?php
$ch = curl_init($url);
curl_setopt_array($ch,
array(CURLOPT_RETURNTRANSFER=>1, CURLINFO_HEADER_OUT=>1));
?>