RE: [PHP] socket_read can not read the whole HTTP page?
Wow, That's correct answer! Thank to Eddie Dunckley ! scheme: $in .= GET {$file} HTTP/1.0\r\n; //--- $in .= Accept: text/html\r\n; $in .= Accept-Language: zh-cn\r\n; $in .= User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 2.0.50727)\r\n; $in .= Host: {$host}\r\n; $in .= Cache-Control: no-cache\r\n; $in .= Connection: closed\r\n\r\n;//must be closed 1.must be HTTP/1.0 : why? IE post HTTP/1.1 . 2. must be closed : so that should be close not closed; Both modification are required. Shall anyone tell me sth. about HTTP/1.0 and HTTP/1.1 ? best regards! ked -Original Message- From: Eddie Dunckley [mailto:[EMAIL PROTECTED] Sent: Friday, December 28, 2007 3:50 PM To: php-general@lists.php.net Subject: Re: [PHP] socket_read can not read the whole HTTP page? On Fri 28 Dec 07, ked wrote: I wrote those script to get HTTP url content, and it works , but it can't read the whole content of the page. Blocked on while ($out = socket_read($socket, 1024)) . $in .= GET {$file} HTTP/1.1\r\n; try to change this to $in .= GET {$file} HTTP/1.0\r\n; $in .= Connection: Keep-Alive\r\n\r\n; and change this to $in .= Connection: closed\r\n\r\n; -- Eddie Dunckley - [EMAIL PROTECTED] - Realtime Travel Connections IBE Development, www.rttc.co.za, cell 083-379-6891, fax 086-617-7831 Where 33deg53'37.23S 18deg37'57.87E Cape Town Bellville Oakdale ZA Chaos, panic, and disorder - my work here is done. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] socket_read can not read the whole HTTP page?
I wrote those script to get HTTP url content, and it works , but it can't read the whole content of the page. Blocked on while ($out = socket_read($socket, 1024)) . browser show the processbar all the time , and the page is not completed display, If I press ESC key to cancel the request , the resource of page show that : the target URL were not read completed . next codes never been executed . Why socket_read blocked? Additional : The network and URL are absolute valid all the time . the size of target page is about 30k bits . script : ? header(Content-type: text/html; charset=UTF-8); error_reporting(E_ALL); echo h2TCP/IP Connection/h2\npre; $service_port = 80; $host = 10.1.1.144; $file = /index.aspx; $address = gethostbyname('10.1.1.144'); /* Create a TCP/IP socket. */ $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket 0) { echo socket_create() failed.\n reason: . socket_strerror($socket) . \n; } else { echo OK.\n; } echo try connect to '$address' : '$service_port'...; $result = socket_connect($socket, $address, $service_port); if ($result 0) { echo socket_connect() failed.\n reason: ($result) . socket_strerror($result) . \n; } else { echo OK.\n; } //$in = HEAD / HTTP/1.1\r\n; $in = ''; $in .= GET {$file} HTTP/1.1\r\n; $in .= Accept: text/html\r\n; $in .= Accept-Language: zh-cn\r\n; //$in .= Accept-Encoding: gzip, deflate\r\n; $in .= User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 2.0.50727)\r\n; $in .= Host: {$host}\r\n; $in .= Cache-Control: no-cache\r\n; $in .= Connection: Keep-Alive\r\n\r\n; echo send HTTP HEAD request...\n{$in}; socket_write($socket, $in, strlen($in)); echo OK.\n; echo read response:---\n\ntextarea; $len = 0; $out= ''; while ($out = socket_read($socket, 1024)) // ---wait for too long time .! { $len += strlen($out); echo $out; } echo /textarea; echo close socket ...; socket_close($socket); echo OK.\n\n; ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] about __get,__set Overloading, read-only properties
I think you misread my pure-hearted thankfulness.I am sorry for my ambiguous sentence. Last reply mean : I'm happy to known that we have the same idea, just like a student got teacher's praise . Merry Christmas Eve! -Original Message- From: Jochem Maas [mailto:[EMAIL PROTECTED] Sent: Sunday, December 23, 2007 9:08 AM To: ked Cc: php-general@lists.php.net Subject: Re: [PHP] about __get,__set Overloading, read-only properties ked schreef: My idea is just your answer...happy..ing ^_^ You are so warmhearted. Thanks a lot! : ) I don't get accused of that very often. are you being sarcastic or did my suggestion help? (sorry I didn't quite understand your reply) I will never post a question to a existing thread . good. that's one down 1,000,983 to go :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Try{} Catch()
use it just like in JAVA, It offers a uniform/good way to manage exception in app. e.g. function getObjFromDB($key) { if (strlen($key)!=0 ) throw new Exception (need condition .); if (!mysql_connect(...)) throw new Exception (can't connect to db .); . } function foo () { $err=''; for (...) { try { getObjFromDB(''); } catch (Exception $e) // if you throw a exception but didn't catch it , the PHP app will stop and not prompt any error message. { echo $e; //output the error message. $err .= $e; } } echo done , total message : {$err}; } -Original Message- From: Al [mailto:[EMAIL PROTECTED] Sent: Monday, December 24, 2007 5:59 AM To: php-general@lists.php.net Subject: Re: [PHP] Try{} Catch() I understand it's intended use and how to use it, have just not found a good use for it yet. Martin Alterisio wrote: It's not supposed to be practical, it's just a way to handle errors. You shouldn't rely on try/catch for algorithm implementation. You create exceptions for errors and unexpected behavior. Then in some other part of the system you use try/catch to prevent the code from terminating abruptly. You catch the exception (you should know which exceptions can be thrown), and act accordingly. Either closing resources, add a log message with debug information, and/or sending an error message to the user. 2007/12/23, Al [EMAIL PROTECTED]: Try() and Catch() seems neat; but, I've not found it to be very practical. Anyone using it? How? Al... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] about __get,__set Overloading, read-only properties
Hi. all , I got a article from php 5.0 manual's comments. It's useful, offer readonly properties for classes. (look at the end of this message for the article ) find out function __construct(), I want to modify $this-id in it , then I got a readonly Exception (defined in __set function). Distinctly, a read-only property could not be change via $obj-attribute = '' , but is could be change via $this-id='', inside of class , isn't it ? How to modify __set function ? thanks for any advises. regards! ked the article is here: Eric Lafkoff (22-Feb-2006 02:56) If you're wondering how to create read-only properties for your class, the __get() and __set() functions are what you're looking for. You just have to create the framework and code to implement this functionality. Here's a quick example I've written. This code doesn't take advantage of the type attribute in the properties array, but is there for ideas. ?php class Test { private $p_arrPublicProperties = array( id = array(value = 4,type = int,readonly = true), datetime = array(value = Tue 02/21/2006 20:49:23,type = string, readonly = true), data = array(value = foo, type = string, readonly = false) ); //ked add!!! public function __construct() { $this-id = 100; //will get exception !! } private function __get($strProperty) { //Get a property: if (isset($this-p_arrPublicProperties[$strProperty])) { return $this-p_arrPublicProperties[$strProperty][value]; } else { throw new Exception(Property not defined); return false; } } private function __set($strProperty, $varValue) { //Set a property to a value: if (isset($this-p_arrPublicProperties[$strProperty])) { //Check if property is read-only: if ($this-p_arrPublicProperties[$strProperty][readonly]) { throw new Exception(Property is read-only); ///---note here return false; } else { $this-p_arrPublicProperties[$strProperty][value] = $varValue; return true; } } else { throw new Exception(Property not defined); return false; } } private function __isset($strProperty) { //Determine if property is set: return isset($this-p_arrPublicProperties[$strProperty]); } private function __unset($strProperty) { //Unset (remove) a property: unset($this-p_arrPublicProperties[$strProperty]); } } $objTest = new Test(); print $objTest-data . \n; $objTest-data = bar; //Works. print $objTest-data; $objTest-id = 5; //Error: Property is read-only. ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] about __get,__set Overloading, read-only properties
My idea is just your answer...happy..ing ^_^ You are so warmhearted. Thanks a lot! : ) I will never post a question to a existing thread . -Original Message- From: Jochem Maas [mailto:[EMAIL PROTECTED] Sent: Friday, December 21, 2007 9:11 AM To: ked Cc: php-general@lists.php.net Subject: Re: [PHP] about __get,__set Overloading, read-only properties pleae don't reply to an existing thread when posting a new question. ked schreef: Hi. all , I got a article from php 5.0 manual's comments. It's useful, offer readonly properties for classes. (look at the end of this message for the article ) find out function __construct(), I want to modify $this-id in it , then I got a readonly Exception (defined in __set function). Distinctly, a read-only property could not be change via $obj-attribute = '' , but is could be change via $this-id='', inside of class , isn't it ? How to modify __set function ? don't - let __set() be the policeman it's supposed to be. either create a private function to initialize values or set the values directly in the array private function init($k, $v) { if (isset($this-p_arrPublicProperties[$k])) $this-p_arrPublicProperties[$k]['value'] = $v; } thanks for any advises. regards! ked the article is here: -- -- Eric Lafkoff (22-Feb-2006 02:56) If you're wondering how to create read-only properties for your class, the __get() and __set() functions are what you're looking for. You just have to create the framework and code to implement this functionality. Here's a quick example I've written. This code doesn't take advantage of the type attribute in the properties array, but is there for ideas. ?php class Test { private $p_arrPublicProperties = array( id = array(value = 4,type = int,readonly = true), datetime = array(value = Tue 02/21/2006 20:49:23,type = string, readonly = true), data = array(value = foo, type = string, readonly = false) ); //ked add!!! public function __construct() { $this-id = 100; //will get exception !! } private function __get($strProperty) { //Get a property: if (isset($this-p_arrPublicProperties[$strProperty])) { return $this-p_arrPublicProperties[$strProperty][value]; } else { throw new Exception(Property not defined); return false; } } private function __set($strProperty, $varValue) { //Set a property to a value: if (isset($this-p_arrPublicProperties[$strProperty])) { //Check if property is read-only: if ($this-p_arrPublicProperties[$strProperty][readonly]) { throw new Exception(Property is read-only); ///---note here return false; } else { $this-p_arrPublicProperties[$strProperty][value] = $varValue; return true; } } else { throw new Exception(Property not defined); return false; } } private function __isset($strProperty) { //Determine if property is set: return isset($this-p_arrPublicProperties[$strProperty]); } private function __unset($strProperty) { //Unset (remove) a property: unset($this-p_arrPublicProperties[$strProperty]); } } $objTest = new Test(); print $objTest-data . \n; $objTest-data = bar; //Works. print $objTest-data; $objTest-id = 5; //Error: Property is read-only. ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] How to new a Object via class name String?
Hi , I'm a freshman in PHP, can anyone give me any advices? I defied some simple classes, like User, Item... in a general way , $obj = new User(); specially, I need to assign a Object via a class name . Now , my code : switch ($className) { case User: return new User(); break ; case Item: return new Item(); break ; default: break ; } I think that It's not a clever job. How to do it skillfully? Thank you for any advice. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] LoadXML trouble
first, create dealxml.php with ansi charset : ?php $r = XML ?xml version=1.0? response customerIDked/customerID appNametickcenter/appName ticketTypetrain/ticketType ticketActionback/ticketAction ticketID1197026188_ec76/ticketID statusKO/status errCode500/errCode errMsgInternal Server Error/errMsg /response XML; $xml = new DOMDocument(); //$xml-validateOnParse = true; $xml-loadXML( $r ); $customer_id = $xml-getElementsByTagName( 'customerID' )-item( 0 )-nodeValue; $app_name = $xml-getElementsByTagName( 'appName' )-item( 0 )-nodeValue; $ticket_type = $xml-getElementsByTagName( 'ticketType' )-item( 0 )-nodeValue; $ticket_action = $xml-getElementsByTagName( 'ticketAction' )-item( 0 )-nodeValue; $ticket_params = $xml-getElementsByTagName( 'parameters' )-item( 0 )-childNodes; echo $customer_id br; echo $app_name br; echo $ticket_typed br; echo $ticket_action br; echo $ticket_params br; ? IE output : Warning: DOMDocument::loadXML() [function.loadXML]: Input is not proper UTF-8, indicate encoding ! in Entity, line: 4 in W:\www\test\dealxml.php on line 26 next , saved the dealxml.php as UTF-8 charset, then it works well. I think that the xml resource should be encoded by utf-8 charset, right or wrong ? My OS : win2000 , simplified chinese. Dose the OS blight DOMDocument ? -Original Message- From: Nathan Nobbe [mailto:[EMAIL PROTECTED] Sent: Tuesday, December 11, 2007 1:25 AM To: Dani CastaƱos Cc: php-general@lists.php.net Subject: Re: [PHP] LoadXML trouble On Dec 10, 2007 12:08 PM, Dani CastaƱos [EMAIL PROTECTED] wrote: Yep, it works when i do saveXML, but not on loadXML step... did the loadXML() method work in the test script i sent over in my last post? if it does then something else is going on when loadXML() is called in the context of your application. The thing is... i'm trying to do something like this: $request = $_POST['xml']; $logger-debug( 'New ticket request' ); /** * Parse XML request to obtain values */ $xml = new DOMDocument(); //$xml-validateOnParse = true; $xml-loadXML( $request ); you should always sanitize input. at the very least you should be running $_POST['xml'] through a call to trim() before handing it to the DOMDocument instance. $request = trim($_POST['xml']); $xml = new DOMDocument(); $xml-loadXML($request); i suspect there is some garbage in or around the string contained in $_POST['xml'] that the DomDocument instance doesnt like, therefore its not behaving the way youd anticipate. for example if you modify the script i sent over last time by putting some spaces in between the xml declaration and the opening tag of the envelope (response) you should see the following error (or something similar [depending on the value of error_reporting]) Warning: DOMDocument::loadXML(): XML declaration allowed only at the start of the document in Entity, line: 3 in /home/nathan/testDom.php on line 19 which consequently leads to ?xml version=1.0? when invoking echo $domDoc-saveXML() . PHP_EOL; rather than ?xml version=1.0? response ticketID1197026188_ec76/ticketID statusKO/status errCode500/errCode errMsgInternal Server Error/errMsg /response which is what you would expect. -nathan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php