View Full Version : offset error - i loop
wolfdogg
02-05-2007, 06:08 PM
I am having a hard tiem with this piece of code for ($i=1; $i <count($lines); $i++)
{
list($key,$val) = explode("=", $lines[$i]);
$keyarray[urldecode($key)] = urldecode($val);
}
heres the current script
$req = 'cmd=_notify-synch';
// Getting the authorization of PDT
$auth_token = "my_auth_token";
$tx_token = $_GET['tx'];
$req .= "&tx=$tx_token&at=$auth_token";
// post back to PayPal system to validate
$header = "";
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
if (!$fp)
{
// HTTP ERROR
echo "$errno, $errstr";
}
else
{
fputs ($fp, $header . $req);
// read the body data
$res = '';
$headerdone = false;
while (!feof($fp))
{
$line = fgets ($fp, 1024);
if (strcmp($line, "\r\n") == 0)
{
// read the header
$headerdone = true;
}
else if ($headerdone)
{
// header has been read. now read the contents
$res .= $line;
}
}
// parse the data
$lines = explode("\n", $res);
$keyarray = array(); //Should be default
//$tokensArray = array(); //Which one is proper? TokensArray should be WRONG
if (strcmp ($lines[0], "SUCCESS") == 0)
{
for ($i=1; $i <count($lines); $i++)
{
list($key,$val) = explode("=", $lines[$i]);
//$keyarray[urldecode($key)] = urldecode($val);
}
$variable= $keyarray['input_name'];
}
else if (strcmp ($lines[0], "FAIL") == 0)
// log for manual investigation
header('Content-type:text/plain');
mail('admin@mysite.com','PDT Test For Thankyou Page',join("\n",$lines));
print_r($lines);
}
fclose ($fp);
Im getting an offset error. this is a wierd kind of error, because the html is echoing instead of executing, and the error is related to a loop which im still trying to figure out loops. its stating that offset 1 is undefined. Does this mean that the loop is running into trouble, or running out of data, or going too far, or not far enough, or what? I think after some research that the offset is a single cycle of the loop, but maybe somebody can clarify.
if the loop error is solved, i think the rest of the things will pan out(ie.. the html displaying instead of executing)
heres the error below, minus all the code thats actually inside the array
and the page is actually displaying live like this (all text)
<pre>
ERRNO: 8
TEXT: Undefined offset: 1
LOCATION: /thankyou.php, line 97, at February 5, 2007, 5:38 pm
Showing backtrace:
</pre><pre>
ERRNO: 8
TEXT: Undefined offset: 1
LOCATION: /thankyou.php, line 97, at February 5, 2007, 5:38 pm
Showing backtrace:
</pre>Array
(
[0] => SUCCESS
[1] => mc_gross=2.00
//blah blah
[36] => residence_country=US
[37] => payment_gross=2.00
[38] =>
)
<!DOCTYPE html PUBLIC "-//W3C//DTD .....
the loop has run dry?
wolfdogg
02-08-2007, 12:49 PM
should that [38] => be blank because it has already errored during calling the data, or is [38] => the blank offset that is causing the error?
scoutt
02-09-2007, 05:51 PM
first, all loops start at 0
$i=0;
not 1.
And why are you parsing the data that way? In the end you will not see the output if IPN is setup correctly. output is irrelavant
And there is no way that there is 97 lines in that code you posted so without seeing the line it is erroring on is real hard to tell.
wolfdogg
02-09-2007, 07:01 PM
this is not IPN, its PDT, which is almost exactly like IPN, except its just for echoing on a thankyou page. so its made for output. does that clarify? or is there still something wrong with the output?
i am trying to echo the output, i have changed the code on that page, please just refer to this code here
$req = 'cmd=_notify-synch';
$tx_token = $_GET['tx'];
$auth_token = "my-auth-token";
$req .= "&tx=$tx_token&at=$auth_token";
// post back to PayPal system to validate
$header = '';
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
// If possible, securely post back to paypal using HTTPS
// Your PHP server will need to be SSL enabled
// $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
if (!fp) {
echo "false";
} else {
fputs ($fp, $header . $req);
// read the body data
$res = '';
$headerdone = false;
while (!feof($fp)) {
$line = fgets ($fp, 1024);
if (strcmp($line, "\r\n") == 0) {
// read the header
$headerdone = true;
}
else if ($headerdone)
{
// header has been read. now read the contents
$res .= $line;
}
}
// parse the data
$lines = explode("\n", $res);
$keyarray = array();
if (strcmp ($lines[0], "SUCCESS") == 0) {
for ($i=1; $i<count($lines);$i++){
list($key,$val) = explode("=", $lines[$i]);
$keyarray[urldecode($key)] = urldecode($val);
}
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment
$firstname = $keyarray['first_name'];
$lastname = $keyarray['last_name'];
$uniqueid = $_GET['tx'];
$custom = $_GET['cm'];
//$itemname = $keyarray[$i]['item_name'];
//$itemname = $_SESSION['ItemName'];
$amount = $keyarray['payment_gross'];
$paymentstatus = $_GET['st'];
}
else if (strcmp ($lines[0], "FAIL") == 0) {
echo "FAIL";
}
}
fclose ($fp); This is the official working script. So, most php shoudl be sound, and note the loop again, begins at 1. i wondered abotut his, but changing it has gotten me nowhere.
So, given that, why woudl i be getting an offset error, and does that error which states offset 1 is an error, does that REALLY mean that ofset 1 is the problem? I believe offset 1 is coming back as payment date, and is url encoded like this [1] => payment_date=19%3a18%3a37+Feb+02%2c+2007+PST does there look like anything wrong with that line or something? i cant figure out where this offset 1 error is coming from. I can tell you, that offset 26 is coming back empty, its stated as [16] => custom= and offset 32 is teh last one, and its showing blank as well, [31] =>
so thats about all i know as far as these offset errors, and i would just supress them if i caould, cause everything is working fine for me, and i dont need those variables anyway. the ones that i need, are already echoing properly.
the other error that im getting on that page that needs supresing, and doesnt seem to be affecting the page... a smarty error? ERRNO: 8 TEXT: Use of undefined constant fp - assumed 'fp' .
scoutt
02-09-2007, 07:10 PM
two things.
using 1 means you skip this one
$lines[0] which is SUCCESS. also you have an error here
if (!fp) {
look real close. what is the line number for offset now?
wolfdogg
02-12-2007, 08:07 PM
hehe, you give me too much credit, i wish, but didnt understand your question about the line for offset. is there some clue in (!fp) or something? or what did you mean?
but also, i think i figured out why the loop starts at 1, because , like you say, 0 is success, and thats the controller, where the loops starts at 1, right? if success then, else if fail then.... so its just reading the first on teh loop, and using to determine what to do, but from there, the loop actually starts at 1, because the word success isnt needed as part of the result loop.
scoutt
02-13-2007, 07:37 AM
ok, look at the red then
if (!$fp) {
wolfdogg
02-16-2007, 04:20 PM
that was too obvious. the ! confused me. i read about what it does before, like 'if not'? but without it i would have recognized that. doh.
this offset error is back.
ERRNO: 8
TEXT: Undefined offset: 1
LOCATION: /home/lvp4sal/public_html/testing/templates_c/%%AF^AF3^AF30FE81%%thankyou_info.tpl.php, line 56, at February 16, 2007, 4:28 pm
Showing backtrace:
-i have changed the code up a bit, ill just repost it. the only error i get now is this offset 1 error.
$req = 'cmd=_notify-synch';
$tx_token = $_GET['tx'];
$auth_token = "my_token";
$req .= "&tx=$tx_token&at=$auth_token";
// post back to PayPal system to validate
$header = '';
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
// If possible, securely post back to paypal using HTTPS
// Your PHP server will need to be SSL enabled
// $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
if (!$fp) {
echo "false";
} else {
fputs ($fp, $header . $req);
// read the body data
$res = '';
$headerdone = false;
while (!feof($fp)) {
$line = fgets ($fp, 1024);
if (strcmp($line, "\r\n") == 0) {
// read the header
$headerdone = true;
}
else if ($headerdone) {
// header has been read. now read the contents
$res .= $line;
}
}
// parse the data
$lines = explode("\n", $res);
$keyarray = array();
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
if (strcmp ($lines[0], "SUCCESS") == 0) {
for ($i=2; $i<count($lines);$i++) {
list($key,$val) = explode("=", $lines[$i]);
$keyarray[urldecode($key)] = @urldecode($val);
}
//Set variables for display
$paymentstatus = $_GET['st'];
$firstname = $keyarray['first_name'];
$lastname = $keyarray['last_name'];
$uniqueid = $_GET['tx'];
$amount = $keyarray['payment_gross'];
//Success Echo thankyou note
echo'<h3>Thank you for your purchase!</h3><b>Payment Details</b><br>'.ucfirst($firstname).' '. ucfirst($lastname).'<br>Payment : '.$paymentstatus.'<br>Amount: '.$amount.'<br>ID# '.$uniqueid.'<br>Please keep this number for your records.';
array#0 is used for a trigger, and the result group that is to be echoed starts at array loop#1, after the trigger which is array loop#0. Is array#0 or array#1 the actual "offset"1? when i look into my inbox, the email looks as if all teh results are echoing, so i suspect that offset1 is referring to array#0, which would point to sometthing malfunctioning on the list() statement, which is the exact line that the error message is pointing to as well.
scoutt
02-16-2007, 07:50 PM
I think you have issues. line 56 of that code you just posted is commented line. Nothing in the few lines above or below will cause that error. Is that the correct code it is erroring on? it seems it is a smarty error as well.
You are missing a closing } for the last if statement
wolfdogg
02-18-2007, 05:22 PM
yeah, the closing bracket was on the rest of the code {/php}
<br>
<p><span class="featureShop">A receipt for your purchase has been emailed.</span></p>
<p>Be sure to check your voice mail, inbox, and junk box periodically for possible replies to your ad's</p>
<p><span class="featureShop">If you have a paypal account, you may also log into your account at <a href="https://www.paypal.com/us/">paypal.com</a> to view details of this transaction</span>.</p>
<form>
<div align="right">
<input name="link2" value="Continue"type="button" id="link" onclick="parent.location=\'?sell\'" />
</div>
</form>
{php}
}
else if (strcmp ($lines[0], "FAIL") == 0)
{//etc...
i guess i forgot to mention, line 56 is actually the line with the list() statement on the post above /\
scoutt
02-18-2007, 05:33 PM
well when you don't show all the code it is hard to diagnosis.
the list is erroring out because of that line doesn't have a = to explode. that is why it fails.
wolfdogg
02-20-2007, 12:35 PM
ok, and what tells you that theres no = sign? are you referring to the results, and the error message, that the offset error is related to no equal sign, rather than an empty value after teh equal sign? or could it be either? at times, i doo see a return result of say 36 =>
which since its blank, theres no equal, but why is the loop going to far if thats teh case? shouldnt it be stopping if theres no variable present, or is that normal to have a result of 36=> which is blank, then the code decides that its blank and ignores it, which would point to it not being able to find the end of the loop? So, how do i get rid of this error? any ideas? also, if i could just supress it instead, as i mentioned, the end result of what i need already, so debugging it would be kind of useless if i could just supress it, unless theres something wrong with the way its setup presently.
i understand you cant supress a @list() so im still trying to figure out just how to get rid of this error. :-)
scoutt
02-20-2007, 07:35 PM
just as I stated. one of the items in the array doesn't have a = sign in it so it can't be split. print out everything in the $lines array.
print_r($lines);
what does it give you?
wolfdogg
02-21-2007, 06:10 PM
hehe, i would if i could get my server to cooperate today... :-) there is apparently a db problem at my host. what a bugger. all this coding in my head, and i cant get any results= :-)
wolfdogg
02-22-2007, 01:32 PM
since the form is working better now, i can finally get these offsets echoed.
here they are
Array ( [0] => SUCCESS [1] => mc_gross=6.00 [2] => item_number1=1792 [3] => tax=0.00 [4] => item_number2=1793 [5] => payer_id=MHNR62DE99PF6 [6] => item_number3=1794 [7] => item_number4=1865 [8] => payment_date=11%3A25%3A53+Feb+22%2C+2007+PST [9] => payment_status=Completed [10] => charset=windows-1252 [11] => mc_shipping=0.00 [12] => mc_handling=0.00 [13] => first_name=jo [14] => mc_fee=0.47 [15] => custom= [16] => payer_status=verified [17] => business=business%40freemoney.com [18] => num_cart_items=4 [19] => mc_handling1=0.00 [20] => mc_handling2=0.00 [21] => mc_handling3=0.00 [22] => mc_handling4=0.00 [23] => payer_email=personal%40freemoney.com [24] => mc_shipping1=0.00 [25] => mc_shipping2=0.00 [26] => mc_shipping3=0.00 [27] => mc_shipping4=0.00 [28] => txn_id=84W81872UT9203803 [29] => payment_type=instant [30] => last_name=bloe [31] => item_name1=Standard+Listing+-+1952+Fordster+Roadster [32] => receiver_email=business%40freemoney.com [33] => item_name2=Basic+Listing+-+1952+Fordster+Roadster [34] => payment_fee=0.47 [35] => item_name3=Basic+Listing+-+1952+Fordster+Roadster [36] => item_name4=Basic+Listing+-+2004+Kawasaki+KX+500 [37] => quantity1=1 [38] => quantity2=2 [39] => receiver_id=64DMFYRADZNLL [40] => quantity3=1 [41] => txn_type=cart [42] => quantity4=1 [43] => mc_gross_1=2.00 [44] => mc_currency=USD [45] => mc_gross_2=2.00 [46] => mc_gross_3=1.00 [47] => residence_country=US [48] => mc_gross_4=1.00 [49] => payment_gross=6.00 [50] => )
ERRNO: 8
TEXT: Undefined offset: 1
looks like its that blasted custom on 16 maybe? or could it be 50? or could it be that 0 doesnt have an = sign, or could it really be 1, even though it doesnt appear to be number 1
scoutt
02-22-2007, 05:59 PM
yeah, it could be that one or the last one. also when you start like this
for ($i=2; $i<count($lines);$i++) {
you are missing mc_gross=6.00, so it will not be complete. that is the most important one.
wolfdogg
02-23-2007, 10:58 AM
oop, i forgot that i left it on 2, thanks! i would have eventually caught that. So, its not the first one then because the loop will start at 1 right? in this case it was 2 :)
scoutt
02-23-2007, 11:19 AM
don't think that
ERRNO: 8
TEXT: Undefined offset: 1
always means it died on the first one. It is a generic error.
wolfdogg
02-23-2007, 03:28 PM
im checking up on this issue here, maybe it will go somewhere http://www.pdncommunity.com/pdn/board/message?board.id=pdt&message.id=434&jump=true#M434
wolfdogg
02-23-2007, 05:54 PM
ok, after doing some testing for ($i=33; $i<count($lines);$i++){ i was able to finally get rid of the error of the offset. And in that case, 32 was the last one. for ($i=32; $i<count($lines);$i++){still had the error. So, what can i do to this code so that it will disregard the last empty set in teh array?
heres the echo Array ( [0] => SUCCESS [1] => payment_date=15%3A49%3A05+Feb+23%2C+2007+PST [2] => txn_type=cart [3] => last_name=bloe [4] => residence_country=US [5] => mc_handling1=0.00 [6] => mc_shipping1=0.00 [7] => payment_gross=2.00 [8] => mc_currency=USD [9] => business=business%40freemoney.com [10] => payment_type=instant [11] => num_cart_items=1 [12] => payer_status=verified [13] => tax=0.00 [14] => payer_email=personal%40freemoney.com [15] => txn_id=1EV68529TW3211921 [16] => receiver_email=business%40freemoney.com [17] => first_name=jo [18] => payer_id=MHNR62DE99PF6 [19] => receiver_id=64DMFYRADZNLL [20] => mc_handling=0.00 [21] => payment_status=Completed [22] => mc_shipping=0.00 [23] => payment_fee=0.36 [24] => mc_fee=0.36 [25] => mc_gross=2.00 [26] => custom=custom [27] => mc_gross_1=2.00 [28] => item_name1=Basic+Listing+-+2004+Kawasaki+KX+500 [29] => charset=windows-1252 [30] => item_number1=1882 [31] => quantity1=2 [32] => ) want that [32] => to be gone, since its empty. or to supress the error caused by it. does anybody have an idea how to change this piece of code?
scoutt
02-23-2007, 06:10 PM
yeah, as long as the last one is ALWAYS empty you can do this
array_pop($lines);
// pop the last element off the end
for ($i=2; $i<count($lines);$i++) {
list($key,$val) = explode("=", $lines[$i]);
$keyarray[urldecode($key)] = @urldecode($val);
}
wolfdogg
02-24-2007, 11:29 AM
I think you meant $i=1
is there a trick you DONT have up your sleeve Scoutt! thats cool. i should have a chance to test this weekend.
This has been the general pattern. Someone asks a question, and they do their best to explain, scoutt has this kryptonite vision and reads into your question doing the math in his head, well his own built in php scripter, and out pops the proper php function on the table!:lol:
thats what i call experience, thanks scoutt!
scoutt
02-24-2007, 12:06 PM
oh yes, I meant 1, I just copied the code from above.
Thanks, well experience is a factor but knowing what functions are available to you is important. The php manual is full of them, it does come in handy. :)
wolfdogg
02-24-2007, 01:48 PM
but thats what i mean, knowing what functions are available to you can only come with experience. Theres always those functions out there that you never heard of , but are perfect for the use, it takes someone that has already been thru this. Sometimes searching thru the manual without knowing the function name can be a task:eek:
vBulletin® v3.6.7, Copyright ©2000-2010, Jelsoft Enterprises Ltd.