We help IT Professionals succeed at work.
Get Started

emulating form post via php backend. what is wrong with new environment?

588 Views
Last Modified: 2012-05-11
hi there,
i have the following document C on Server C, to capture the data and send off to document A on Server A via a post request, as well as document B on Server B. the do_post_request is without curl, and is shared here: HTTP POST from PHP, without cURL
<?php
// required functions
function do_post_request($url, $data, $optional_headers = null) {
	$params = array('http' => array(
			'method' => 'post',
			'content' => $data
		)
	);
	if ($optional_headers!== null) {
		$params['http']['header'] = $optional_headers;
	}
	$ctx = stream_context_create($params);
	$fp = @fopen($url, 'rb', false, $ctx);
	if (!$fp) {
		throw new Exception("Problem with $url, $php_errormsg");
	}
	$response = @stream_get_contents($fp);
	if ($response === false) {
		throw new Exception("Problem reading data from $url, $php_errormsg");
	}
	return $response;
}
// emulate form submissions to leads 360 and intellisource
$url = "http://www.intellisource.co.za/999_andycabenquiries.php";		// intellisource request
$data = "name=".$_POST["name"].											// First Name
		"&surname=".$_POST["surname"].									// Last Name
		"&email=".$_POST["email"].										// Email
		"&textfield32=".$_POST["textfield32"].							// Phone
		"&cellphone=".$_POST["cellphone"].								// Cellphone
		"&message=".$_POST["message"];									// Message
$ret = do_post_request($url,$data);										// post intellisource request id
echo $ret;																// print intellisource return value
$url  = "https://secure.leads360.com/Import.aspx?Provider=Vertical".	// leads 360 url
		"Masters-Other&Client=LMX-9014&CampaignId=7075";
$data = "Ref Id= ".														// Lead Provider ID
		"&First Name=".$_POST["name"].									// First Name
		"&Last Name=".$_POST["surname"].								// Last Name
		"&Email=".$_POST["email"].										// Email
		"&Home Phone=".$_POST["textfield32"].							// Home Phone
		"&Work Phone=".$_POST["textfield32"].							// Work Phone
		"&Mobile Phone=".$_POST["cellphone"].							// Mobile Phone
		"&Message=".$_POST["message"].									// Message
		"&AndyCab ID=".$ret;											// add andycab id retrieved from intellisource
$ret = do_post_request($url,$data);										// post leads 360 request
echo $ret;																// print leads 360 return value
?>

Open in new window

now with the current configuration of server A, this code works posting to both server A and server B when testing from server A - but with the unknown configuration of server C, i need to figure out why i am getting the following exception and if there is a way to get any more details when running. the response on server C:
<br />
<b>Fatal error</b>:  Uncaught exception 'Exception' with message 'Problem with http://www.intellisource.co.za/999_andycabenquiries.php, ' in /usr/www/users/andyca/intellisourceleads360.php:15
Stack trace:
#0 /usr/www/users/andyca/intellisourceleads360.php(35): do_post_request('http://www.inte...', 'name=Pierre&amp;sur...')
#1 /usr/www/users/andyca/form.php(5): include_once('/usr/www/users/...')
#2 {main}
  thrown in <b>/usr/www/users/andyca/intellisourceleads360.php</b> on line <b>15</b><br />

Open in new window

the A server is www.intellisource.co.za which is on an Apache 2.2.19 machine.
the B server is www.leads360.co.za which seems to be on an IIS machine.
the C server is www.andycab.co.za which is on an Apache 2.2.9 machine.
server A posts to server B and itsself, but server C does not post to either it seems.
document A which captures the data, and inserts into a database table, is the following:
<?php
$GLOBALS["sql"] = array();
$conn = mysql_connect("localhost", "dwtphovu_f3rr37y", "JapieMySkapie101");
if (!$conn) {
	$err = "ERROR #".mysql_errno()."Could not connect: ".mysql_error()." in ".$doc." on Line ".$line;
	array_push($GLOBALS["sql"],$err);
}
mysql_select_db("dwtphovu_8347379386_prod", $conn);
function mysql_query_errors($sql, $conn, $doc, $line, $expret = false) {
	$line--;
	$err = "";
	$result = mysql_query($sql);
	if (mysql_errno() > 0) {
		$err = "ERROR #".mysql_errno().": ".mysql_error()." in ".$doc." on Line ".$line;
	} elseif ($expret) {
		if (is_null($result)) {
			$err = "NOTICE #0504: A null value was returned by the query defined in ".$doc." on line ".$line--;
		} elseif ($result === false) {
			$err = "NOTICE #0502: A false value was returned by the query defined in ".$doc." on line ".$line--;
		} elseif ($result === true) {
			$err = "NOTICE #0501: A true value was returned by the query defined in ".$doc." on line ".$line--;
		} elseif (mysql_num_rows($result) < 1) {
			$err = "NOTICE #0500: An empty result set was returned by the query defined in ".$doc." on line ".$line--;
		}
		array_push($GLOBALS["sql"],$sql."\n",$err."\n");
	}
	return $result;
}
/* **************************************************************************************************
function str_in_array($haystack, $needle, $sw = 1) {
    foreach ($haystack as $value[0] => $value[1]) {
        if (stristr($value[$sw],$needle)!==FALSE) {
            return true;
        }
    }
    return false;
}
// get table fields to compare with form
$existing = array();
$tsql = "SHOW COLUMNS FROM dwtphovu_8347379386_prod.999_andycabenquiries;";
$result = mysql_query_errors($tsql, $conn, __FILE__, __LINE__);
if ($result) {
	while ($row = mysql_fetch_assoc($result)) {
		if ($row["Field"] != "bigint_EnquiryID") {
			array_push($existing,$row["Field"]);
		}
	}
}
echo "\$existing = ".print_r($existing,true)."<br>";
// alter table to add new fields if found
$tocreate = array();
foreach ($_POST as $key=>$value) {
	if ($key != "Reset" &&
		$key != "Submit2" &&
		!str_in_array($existing,$key)) {
		array_push($tocreate,ucfirst($key));
	}
}
echo "\$tocreate = ".print_r($tocreate,true)."<br>";
foreach ($tocreate as $key=>$value) {
	$asql = "ALTER TABLE 
				dwtphovu_8347379386_prod.999_andycabenquiries 
			 ADD 
				`text_Enquiry".$value."` 
				TEXT CHARACTER SET utf8 
				COLLATE utf8_unicode_ci 
				NULL 
				DEFAULT NULL 
				COMMENT 'Enquiry ".$value."',
			  ADD FULLTEXT 'FULLTEXT' (
				`text_Enquiry".$value."`
			 )";
	$result = mysql_query_errors($asql, $conn, __FILE__, __LINE__);
}
echo print_r($GLOBALS["sql"],true);
************************************************************************************************** */
// insert new record
$tsql = "INSERT INTO 
			 dwtphovu_8347379386_prod.999_andycabenquiries 
		 (
			 text_EnquiryName, 
			 text_EnquirySurname, 
			 text_EnquiryEmail, 
			 text_EnquiryPhone, 
			 text_EnquiryCellPhone, 
			 text_EnquiryMessage
		 ) VALUES (
			\"".mysql_real_escape_string($_POST["name"])."\", 
			\"".mysql_real_escape_string($_POST["surname"])."\", 
			\"".mysql_real_escape_string($_POST["email"])."\", 
			\"".mysql_real_escape_string($_POST["textfield32"])."\", 
			\"".mysql_real_escape_string($_POST["cellphone"])."\", 
			\"".mysql_real_escape_string($_POST["message"])."\"
		 );";
$result = mysql_query_errors($tsql, $conn, __FILE__, __LINE__);
echo (count($GLOBALS["sql"]))?implode("\n",$GLOBALS["sql"]):mysql_insert_id($conn);

Open in new window

document B is a third party lead manager.
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE