By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

PHP post + echo issues

P: 90
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $td_sku="1088963";
  3.  
  4. //Following the XML data
  5. $xmldata="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r
  6. <OnlineCheck>\r
  7. <Header>\r
  8. <BuyerAccountId>----------</BuyerAccountId>\r
  9. <AuthCode>----------</AuthCode>\r
  10. <Type>Full</Type>\r
  11. </Header>\r
  12. <Item line=\"1\">\r
  13. <ManufacturerItemIdentifier/>\r
  14. <ResellerItemIdentifier/>\r
  15. <DistributorItemIdentifier>$td_sku</DistributorItemIdentifier>\r
  16. <Quantity>1</Quantity>\r
  17. </Item>\r
  18. </OnlineCheck>";
  19. $fp = fsockopen("somewebsite.com", 8080, $errno, $errstr, 30);
  20.  
  21. //Generate the postdata on a valid way, $out4 needs to be calculated, so will be later.
  22. $out1 = "POST /Onlchk HTTP/1.0\r
  23. ";
  24. $out2 = "Content-Type: multipart/form-data; boundary=---------------------------2\r
  25. ";
  26. $out3 = "Host: somewebsite.com:8080\r
  27. ";
  28. $out5 = "Connection: close\r
  29. \r
  30. ";
  31. $out6 = "-----------------------------2\r
  32. ";
  33. $out7 = "Content-Disposition: form-data; name=\"onlinecheck\"\r
  34. \r
  35. ";
  36. $out8 = "\r
  37. -----------------------------2--";
  38.  
  39. //Calculation of the Content-Length:
  40. $tlen=strlen($out6)+strlen($out7)+strlen($xmldata)+strlen($out8);
  41. $out4 = "Content-Length: $tlen\r
  42. ";
  43.  
  44. //Generate full output
  45. $out = $out1.$out2.$out3.$out4.$out5.$out6.$out7.$xmldata.$out8;
  46. fwrite($fp, $out);
  47. $retval = "";
  48. while(!feof($fp)){$retval = "$retval".fgets($fp,128);}
  49. fclose($fp);
  50. list($headers,$body) = explode("<?xml version=\"1.0\" encoding=\"UTF-8\"?>",$retval);
  51. $doc = new DOMDocument();
  52. $doc ->LoadXML($body);
  53. $item_ids = $doc->getElementsByTagname( "OnlineCheck" );
  54. foreach( $item_ids as $item )
  55. {
  56. // $error_s = $item->getElementsByTagName( "Errorstatus" ); $tderror = $error_s->item(0)->nodeValue;
  57. $stock_s = $item->getElementsByTagName( "AvailabilityTotal" ); $tdstock = $stock_s->item(0)->nodeValue;
  58. // Reformatting the price to ISO compatible format
  59. $price_s = $item->getElementsByTagName( "UnitPriceAmount" ); $tdprice = str_replace('.','',$price_s->item(0)->nodeValue);
  60. $tdprice=str_replace(',','.',$tdprice);
  61. }
  62. //Rest of handling off the data under here
  63.  
  64. echo "Stock: ".$tdstock."<BR />";
  65. echo "Price: ".&tdprice;
  66. ?>
Besides removing the account number and website for security, all I've done to this code is attempt to echo the results of the returned data.
All that is returned, is a completely blank page.
I've tried adding
Expand|Select|Wrap|Line Numbers
  1. error_reporting(E_ALL);
  2. ini_set('display_errors', true);
And still, the page is completely blank.

I added a few echo's scattered at various parts and got results. I echoed $td_sku; $xml and $out, all just after they were 'created', so I know that the echo code I was using worked. I think there must be something below the $retval that's wrong.
I'm sure I had it working the other day; but I had the php hosted on a different site which I can't access at the moment.

Can anyone see anything obviously wrong; or suggest something to try?

Thanks
Mandi
Oct 8 '08 #1
Share this Question
Share on Google+
8 Replies


Atli
Expert 5K+
P: 5,058
Hi.

Are you sure the request is valid?
How does the $out variable look before you use it?

You could try using the Curl functions to do this.
That would ensure that your request is in fact valid.

If this was working on a previous server, but not on a new server, then what are the differences between the servers?
Are all the required extensions available?

I know some hosts limit the use of sockets and other similar things.
Could that be the problem?
Oct 8 '08 #2

pbmods
Expert 5K+
P: 5,821
Heya, Mandi.

Just want to make sure: Is the "echo "Price: ".&tdprice;" in the last line a typo?
Oct 9 '08 #3

P: 90
Heya, Mandi.

Just want to make sure: Is the "echo "Price: ".&tdprice;" in the last line a typo?
Oops. yeah.
I was using
Expand|Select|Wrap|Line Numbers
  1. echo $tdprice
  2. echo $tdstock
I know it outputs without spaces but that's fine for just making sure it worked.


Are you sure the request is valid?
No; I'm just assuming it is as it's the code I got given by the guys that set it up.

How does the $out variable look before you use it?
Expand|Select|Wrap|Line Numbers
  1. POST /Onlchk HTTP/1.0 Content-Type: multipart/form-data; boundary=---------------------------2 Host: somewebsite.com:8080 Content-Length: 501 Connection: close -----------------------------2 Content-Disposition: form-data; name="onlinecheck" xxxxxx xxxx-xxxx-xxxx Full 1088963 1 -----------------------------2--
In the above xxxxxx and xxxx-xxxx-xxxx have been edited by me as they are usernames and password.

what are the differences between the servers?
None. They are identical in both hardware, software and setup which is confusing.

Are all the required extensions available?
I don't know what they are to be honest.

I know some hosts limit the use of sockets and other similar things.
Could that be the problem?
On my side, the servers are identical so I assume if it works on one...
On their side, like I said, the code is theirs and I'd guess they'd only post working code.
Oct 9 '08 #4

P: 90
Okay so maybe I was wrong.
Something must be different between the two.

I've just copy&pasted the code from the working server to the non-working server, and it doesn't work!

I've checked the working one, and it still works.
WTH?!



Anyway, I guess the code's okay.
Is there anyway to have a pop-up box in PHP (like the [msgbox "text here"] in VBA)?
Oct 9 '08 #5

Atli
Expert 5K+
P: 5,058
Okay so maybe I was wrong.
Something must be different between the two.

I've just copy&pasted the code from the working server to the non-working server, and it doesn't work!

I've checked the working one, and it still works.
WTH?!



Anyway, I guess the code's okay.
The strange thing is, when PHP is missing an extension or something like that, it usually tells you about it. A blank page, even after having turned on error_reporting, is very unusual.

There is one thing that could explain this.
Try checking if the socket was really opened.
Like putting this after the fsocketopen call:
Expand|Select|Wrap|Line Numbers
  1. if (!$fp) {
  2.     echo "$errstr ($errno)<br />\n";
  3.  
Should tell you if the socket connection failed.

It is also possible that a firewall on your new server is blocking the connection.

Is there anyway to have a pop-up box in PHP (like the [msgbox "text here"] in VBA)?
No, not really. PHP is server side so there is no direct connection between the PHP code and the client computer.

You could always just echo JavaScript alert windows tho:
Expand|Select|Wrap|Line Numbers
  1. echo "<script type='text/javascript'>alert(". $something .");</script>";
But those wouldn't necessarily be sent at once. PHP can cache the output and send it in chunks.
Oct 9 '08 #6

P: 90
Expand|Select|Wrap|Line Numbers
  1. if (!$fp) {
  2.     echo "$errstr ($errno)<br />\n";
  3.  
Still a blank page I'm afraid.

I'll try and explain the setup of the computers a little.
The two I'm testing on, are actually trixbox (phone servers); but they have a directory for webpages as they have a web interface. I've just stuck the php into a file called c2ktest.php on there (same place on both).
These are then behind a hardware firewall and connected to windows 2003 small business server for the IP handouts etc.

trixbox uses a Linux OS (CentOS 5 I believe).

I'm calling the script from my PC using http://trixboxIP/c2ktest.php
One it works, the other it doesn't.

I've added a 'get' to replace the hardcoded SKU, which works (on the one that works anyway).

I think there must be a package or module that I have installed on one and not on the other. It's the only thing I can think of that might be messing this up.
So I'm going to go through the 500 or so packages and see which box has which packages. (grep is such a useful command).
I've already checked ones with 'php' in their name, and they're identical on both machines.

Unfortunately, if I am right about the packages being the problem, it'll be tough for anyone to help me unless they too use trixbox and can give me an idea of where I should be looking.

So thanks for your help, and if you've got any more ideas I'll be happy to try them.

Mandi
Oct 9 '08 #7

P: 90
Ah wait. I got something.
Expand|Select|Wrap|Line Numbers
  1. Resource id #2
I tried echo $fp and that appeared.
Anyone?
Oct 9 '08 #8

P: 90
Ta da!
It works =)

Thanks all for your help =)

Now to add a variable and a quick GET/REQUEST command and viola.
Oct 10 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.