TCP/IP, HTTP Connection for XML retrieval

error_reporting (E_ALL);


/* Get the IP address for the target host. */
$address = gethostbyname ('');

/* Create a TCP/IP socket. */
$socket = fsockopen($address,80, &$errno, &$errstr, 30);
if ($socket < 0) {
    echo "Socket() failed"; 
} else {
    echo "Socket() successful";

echo "Attempting to connect to '$address' on port 80";

$in = "GET /data/current_obs/KEWR.xml HTTP/1.1\r\n";
$in .= "Host:\r\n";
$in .= "Connection: close\r\n\r\n";
$out = '';

echo "Sending HTTP GET request...";
fwrite ($socket, $in, strlen ($in));
echo "OK.\n";

echo "Reading response:\n\n";
$xmlBuffer = "";
while ($out = fread ($socket, 2048)) {
    $xmlBuffer .= $out;

$bgn = strpos($xmlBuffer, "<temperature_string>") 
		          + strlen("<temperature_string>");
$end = strpos($xmlBuffer, "</temperature_string>");

$temperature = substr($xmlBuffer,$bgn,$end-$bgn);
echo "<h1>$temperature</h1>";

echo "Closing socket...";
fclose ($socket);
echo "OK.\n";

The above code is a technique for downloading an XML file from the National Weather Service that contains information about Newark's weather. In this case, I am searching for the tag named temperature_string. After finding the boundary positions for the beginning and ending tag, I put the value given for the temperature into a variable and print it back to the browser. The rest of the data is ignored.

The technique used shows the way requests are sent by browsers to the web server using HTTP protocol. After sending the request, we loop through a While() structure to fill the xmlBuffer variable with the data sent by the server.

How can you improve this script?