Jump to content

Looking for a more efficient way to write this script


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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