Jump to content

Looking for a more efficient way to write this script


taylor

Recommended Posts

Below is a simple script to open a connection and retrieve information.  It works currently works great, but I am looking for a way to make it more efficient.  Especially the $xml_response variable. 

 

Is there a way to do this so the script doesn't have to open and close the socket connection for every variable I am creating with the between(); function?  The first time the GetRecipientData function is called it returns an xml response with all the data that is needed.  It is stored in the $buffer variable.

 

<?php
session_start();
$_SESSION['email'] = $_GET['email'];

function GetRecipientFields () {

$list_id = $_SESSION['list_id'];
$email = $_SESSION['email'];

$host='host.domain.com';
$servlet='XMLAPI';
$port='80'; 
$time_out='20';

$sock = fsockopen ($host, $port, $errno, $errstr, $time_out);

if (!$sock) { 
	print("Could not connect to host:". $errno . $errstr); 
	return (false); 
}

$data = "xml=<?xml version=\"1.0\"?><Envelope><Body>"; 
$data .= "<SelectData><LIST_ID>" . $list_id . "</LIST_ID>"; 
$data .= "<EMAIL>" . $email . "</EMAIL>"; 
$data .= "</SelectData></Body></Envelope>"; 
$size = strlen ($data); 

fputs ($sock, "POST /servlet/" . $servlet . " HTTP/1.1\n"); 
fputs ($sock, "Host: " . $host . "\n"); 
fputs ($sock, "Content-type: application/x-www-form-urlencoded\n"); 
fputs ($sock, "Content-length: " . $size . "\n"); 
fputs ($sock, "Connection: close\n\n"); 
fputs ($sock, $data); 
$buffer = ""; 

while (!feof ($sock)) { 
  	$buffer .= fgets ($sock); 
}

fclose ($sock); 
return ($buffer);

}

function between ($this, $that, $inthat) { 
  return before($that, after($this, $inthat)); 
} 

function before ($this, $inthat) { 
    return substr($inthat, 0, strpos($inthat, $this)); 
} 

function after ($this, $inthat) { 
    if (!is_bool(strpos($inthat, $this))) 
    return substr($inthat, strpos($inthat,$this)+strlen($this)); 
} 

// XML Parsing of GetRecipientFields(); function
$xml_response = GetRecipientFields();

$first_name = between ("<NAME>FIRST_NAME</NAME>\n<VALUE>","</VALUE>", $xml_response); 
$last_name = between ("<NAME>LAST_NAME</NAME>\n<VALUE>","</VALUE>", $xml_response);
$street_address = between ("<NAME>ADDR1</NAME>\n<VALUE>","</VALUE>", $xml_response);
$city = between ("<NAME>CITY</NAME>\n<VALUE>","</VALUE>", $xml_response);
$state = between ("<NAME>STATE</NAME>\n<VALUE>","</VALUE>", $xml_response);
$postal_code = between ("<NAME>ZIP</NAME>\n<VALUE>","</VALUE>", $xml_response);

?>

You are only opening a socket once with the GetRecipient function. The rest just parse the return data, so yea. You are already only opening/closing the connection once in that code, as there is no loop in the data.

 

If I missed something, sorry. Been away for a while, but as far as I know it looks alright and only does 1 connection.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.