5 Replies - 585 Views - Last Post: 14 August 2008 - 01:24 AM Rate Topic: -----

#1 aodat2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-August 08

PHP Question - Noobie

Post icon  Posted 13 August 2008 - 05:33 AM

I was given a code which I am suppose to incorporate into my website.

Can someone be kind enough to help me by telling me what I should do here to make the code work? I do understand that in the end of the Function, it will return a value or something like that which is in the variable $Result but can someone tell me what else I am suppose to do to get the Function working or how am I suppose to call the Function?

Thanks a lot for the help in advance.

<?PHP
function Requery(){
$query = "http://www.yourdomain.com/?MerchantCode=" .
$MerchantCode . "&RefNo=" . $RefNo . "&Amount=" . $Amount;
$url = parse_url($query);
$host = $url["host"];
$path = $url["path"] . "?" . $url["query"];
$timeout = 1;
$fp = fsockopen ($host, 80, $errno, $errstr, $timeout);
if ($fp) {
fputs ($fp, "GET $path HTTP/1.0\nHost: " . $host . "\n\n");
while (!feof($fp)) {
$buf .= fgets($fp, 128);
}
$lines = split("\n", $buf);
$Result = $lines[count($lines)-1];
fclose($fp);
} else {
# enter error handing code here
}
return $Result;
}
?>


Is This A Good Question/Topic? 0
  • +

Replies To: PHP Question - Noobie

#2 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: PHP Question - Noobie

Posted 13 August 2008 - 05:45 AM

Assuming this is for an ecommerce site, you will need to hard code the $MerchantCode (your clients merchant number from the bank), the $RefNo - not sure what $RefNo is for, and the Amount, which I'm guessing is a total amount to be payed. I would change the first two lines of the function to this:
function Requery($Amount, $RefNo){
	$query = "http://www.yourdomain.com/?MerchantCode=190283123&RefNo=".$RefNo."&Amount=" . $Amount;


And you call it like this:
<?php Requery(9.99, 1293123); // Where 9.99 is the amount and 1293123 is the reference number 
?>



That is assuming the function is for passing data to the merchant portal.
Was This Post Helpful? 0
  • +
  • -

#3 aodat2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-August 08

Re: PHP Question - Noobie

Posted 13 August 2008 - 07:14 AM

Oh ok, I think I got it working already.

The only problem I have now is what's left unknown. I have the variables $MerchantCode and $RefNo but I do not have the amount.

Could you tell me how I could obtain a value from a MySQL table?

Example:
Payment_Table contains the fields Name, Address, Amount, RefNo and Transaction_ID.

How could I get the variable from the MySQL table out so that I could use it?

Thanks for the help in advance.
Was This Post Helpful? 0
  • +
  • -

#4 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: PHP Question - Noobie

Posted 13 August 2008 - 07:52 AM

Normally I'd say to check out a SQL tutorial on this site but it sounds like you may be in a rush so here's a helping hand :)

$sql = 'SELECT Name, Address, Amount, RefNo, Transaction_ID FROM Payment_Table'; // Will get ALL rows in Payment_Table
$result = mysql_query($sql)or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ // WILL LOOP THROUGH EACH ROW 1 AT A TIME
	$name = $row['Name'];
	$address = $row['Address'];
	$amount = $row['Amount'];
	$ref = $row['RefNo'];
	$transid = $row['Transaction_ID'];
}



You will probibly want to be pulling just one result at a time - let me know how you get on and I'll try and help you through it
Was This Post Helpful? 0
  • +
  • -

#5 aodat2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-August 08

Re: PHP Question - Noobie

Posted 13 August 2008 - 09:36 AM

Thank you so much pemcconnell. I got it working already.

Now, the funniest part is that I am stuck with a function that won't work for some crazy reason. It always give me "Invalid Parameters" which makes me think that something is wrong but if you enter the URL directly into the browser, everything shows up correctly.

Can someone please help on this? Where did I make a mistake?

$MerchantCode = "12345";
$RefNo = "22";
$Amount_Paid = "1.00";

function Requery(){
$query = "http://www.yourdomain.com/enquiry.asp?MerchantCode=" .
$MerchantCode . "&RefNo=" . $RefNo . "&Amount=" . $Amount_Paid;
$url = parse_url($query);
$host = $url["host"];
$path = $url["path"] . "?" . $url["query"];
$timeout = 1;
$fp = fsockopen ($host, 80, $errno, $errstr, $timeout);
if ($fp) {
fputs ($fp, "GET $path HTTP/1.0\nHost: " . $host . "\n\n");
while (!feof($fp)) {
$buf .= fgets($fp, 128);
}
$lines = split("\n", $buf);
$Result = $lines[count($lines)-1];
fclose($fp);
} else {
# enter error handing code here
}
return $Result;
}

$verify = Requery();
echo $verify;

This post has been edited by aodat2: 13 August 2008 - 09:37 AM

Was This Post Helpful? 0
  • +
  • -

#6 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: PHP Question - Noobie

Posted 14 August 2008 - 01:24 AM

I'm not getting any invalid parameters error with that code.

It won't return anything at the moment because:

$Result = $lines[count($lines)-1];



Will return nothing as the last few indexes of the array $lines are empty.

This line will return something however:

$Result = $lines[count($lines)-5];



To see all the results this function will assign to the array $lines, use the following code:

<?php
$MerchantCode = "12345";
$RefNo = "22";
$Amount_Paid = "1.00";

function Requery(){
	$query = "http://www.yourdomain.com/enquiry.asp?MerchantCode=" .
	$MerchantCode . "&RefNo=" . $RefNo . "&Amount=" . $Amount_Paid;
	$url = parse_url($query);
	$host = $url["host"];
	$path = $url["path"] . "?" . $url["query"];
	$timeout = 1;
	$fp = fsockopen ($host, 80, $errno, $errstr, $timeout);
	if ($fp) {
		fputs ($fp, "GET $path HTTP/1.0\nHost: " . $host . "\n\n");
		while (!feof($fp)) {
			$buf .= fgets($fp, 128);
		}
		$lines = split("\n", $buf);
		//$Result = $lines[count($lines)-1];
		$Result = print_r($lines);
		fclose($fp);
	} else {
		$Result = 'There was an error';
	}
	return $Result;
}

echo Requery();
?>

If you are getting that error, it'll probibly be from a different function, or a different call (i.e.) Requery(123231); Where you have a parameter being fed to the function when it doesn't need to be.


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1