[PHP] Parsing AJAX post data -- The Way
Just wondering what smart people do for parsing data sent by the Javascript XMLHTTP object--e.g., http.send("post",url,true)... In a normal form submit, the $_POST global nicely allocates form elements as array elements automatically. But with the AJAX way, the data get stuffed inside $HTTP_RAW_POST_DATA as a string, thereby making extraction more tedious. Any ideas? ...Rene -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Parsing AJAX post data -- The Way
Just wondering what smart people do for parsing data sent by the Javascript XMLHTTP object--e.g., http.send("post",url,true)... In a normal form submit, the $_POST global nicely allocates form elements as array elements automatically. But with the AJAX way, the data get stuffed inside $HTTP_RAW_POST_DATA as a string, thereby making extraction more tedious. Any ideas? ...Rene -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Parsing AJAX post data -- The Way
On 25-Jan-07, at 7:49 AM, Myron Turner wrote: [EMAIL PROTECTED] / 2007-01-24 23:41:19 -0700: Just wondering what smart people do for parsing data sent by the Javascript XMLHTTP object--e.g., http.send("post",url,true)... In a normal form submit, the $_POST global nicely allocates form elements as array elements automatically. But with the AJAX way, the data get stuffed inside $HTTP_RAW_POST_DATA as a string, thereby making extraction more tedious. Try setting this header before sending your Ajax request: http_request.setRequestHeader("Content-type", "application/x-www- form-urlencoded"); Then $_POST should have an array, as expected. But $HTTP_RAW_POST_DATA will not be available. Yes, that is the trick, thank you. I didn't realize sending the correct header would then make $_POST do it's magic with "&var=arg into an array", but now it works as desired. Thanks everyone for the assistance. ...Rene -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Parsing AJAX post data -- The Way
On 25-Jan-07, at 4:46 PM, Richard Lynch wrote: On Thu, January 25, 2007 12:41 am, M5 wrote: Just wondering what smart people do for parsing data sent by the Javascript XMLHTTP object--e.g., http.send("post",url,true)... In a normal form submit, the $_POST global nicely allocates form elements as array elements automatically. But with the AJAX way, the data get stuffed inside $HTTP_RAW_POST_DATA as a string, thereby making extraction more tedious. Call me crazy, but if AJAX is sending POST data correctly, your PHP code shouldn't have to do anything special... You're right in that "*IF* AJAX is sending POST data correctly" everything is okay--that is, will $_POST contain the posted data as array elements. POST data is POST data. The $HTTP_RAW_POST_DATA should be there as well, if you turned that on, but that doesn't make $_POST go away. Actually, that's not true. If the POST data is not set with the correct headers... http.setRequestHeader('Content-Type', 'application/x-www-form- urlencoded'); http.setRequestHeader("Content-length", payload.length); http.setRequestHeader("Connection", "close"); ...then $_POST will be empty and the data that is sent can only be accessed from $HTTP_RAW_POST_DATA (which incidentally is off by default). That was my problem--I wasn't sending those http headers. An earlier poster pointed it out to me, and that solved the problem. ...Rene -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Find midpoint between two points
I found a nice javascript function that takes two points of latitude and longitude and returns a midpoint. I'm now trying to rewrite in PHP, but having some problems. Here's the original javascript function, taken from http://www.movable-type.co.uk/scripts/ LatLong.html : LatLong.midPoint = function(p1, p2) { var dLon = p2.lon - p1.lon; var Bx = Math.cos(p2.lat) * Math.cos(dLon); var By = Math.cos(p2.lat) * Math.sin(dLon); lat3 = Math.atan2(Math.sin(p1.lat)+Math.sin(p2.lat), Math.sqrt((Math.cos(p1.lat)+Bx)*(Math.cos(p1.lat) +Bx) + By*By ) ); lon3 = p1.lon + Math.atan2(By, Math.cos(p1.lat) + Bx); if (isNaN(lat3) || isNaN(lon3)) return null; return new LatLong(lat3*180/Math.PI, lon3*180/Math.PI); } And here's my PHP variant, which isn't working: function midpoint ($lat1, $lng1, $lat2, $lng2) { $dlng = $lng2 - $lng1; $Bx = cos($lat2) * cos($dlng); $By = cos($lat2) * sin($dlng); $lat3 = atan2( sin($lat1)+sin($lat2), sqrt((cos($lat1)+$Bx)*(cos ($lat1)+$Bx) + $By*$By )); $lng3 = $lng1 + atan2($By, (cos($lat1) + $Bx)); $pi = pi(); return ($lat3*180)/$pi .' '. ($lng3*180)/$pi; } Any ideas why it's returning wrong values? ...Rene -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Find midpoint between two points
Thanks for pointing out the PHP's deg2rad requirement. That was the problem. ...Rene On 7-Feb-07, at 7:30 PM, Gregory Beaver wrote: M5 wrote: I found a nice javascript function that takes two points of latitude and longitude and returns a midpoint. I'm now trying to rewrite in PHP, but having some problems. Here's the original javascript function, taken from http://www.movable-type.co.uk/scripts/LatLong.html : LatLong.midPoint = function(p1, p2) { var dLon = p2.lon - p1.lon; var Bx = Math.cos(p2.lat) * Math.cos(dLon); var By = Math.cos(p2.lat) * Math.sin(dLon); lat3 = Math.atan2(Math.sin(p1.lat)+Math.sin(p2.lat), Math.sqrt((Math.cos(p1.lat)+Bx)*(Math.cos(p1.lat)+Bx) + By*By ) ); lon3 = p1.lon + Math.atan2(By, Math.cos(p1.lat) + Bx); if (isNaN(lat3) || isNaN(lon3)) return null; return new LatLong(lat3*180/Math.PI, lon3*180/Math.PI); } And here's my PHP variant, which isn't working: function midpoint ($lat1, $lng1, $lat2, $lng2) { $dlng = $lng2 - $lng1; $Bx = cos($lat2) * cos($dlng); $By = cos($lat2) * sin($dlng); $lat3 = atan2(sin($lat1)+sin($lat2), sqrt((cos($lat1)+$Bx)*(cos($lat1)+$Bx) + $By*$By )); $lng3 = $lng1 + atan2($By, (cos($lat1) + $Bx)); $pi = pi(); return ($lat3*180)/$pi .' '. ($lng3*180)/$pi; } Any ideas why it's returning wrong values? Are you converting from degrees to radians? With identical input, the javascript function is identical to the PHP function (I tested to verify) I got this by reading at the bottom of the page: "* Notes: trig functions take arguments in radians, so latitude, longitude, and bearings in degrees (either decimal or degrees/minutes/seconds) need to be converted to radians, rad = π.deg/180. When converting radians back to degrees (deg = 180.rad/π), West is negative if using signed decimal degrees. For bearings, values in the range -π to +π (-180° to +180°) need to be converted to 0 to +2π (0°–360°); this can be done by (brng+2.π)%2.π where % is the modulo operator. View page source to see JavaScript functions to handle these conversions. * The atan2() function widely used here takes two arguments, atan2(y, x), and computes the arc tangent of the ratio y/x. It is more flexible than atan(y/x), since it handles x=0, and it also returns values in all 4 quadrants -π to +π (the atan function returns values in the range -π/2 to +π/2). * If you implement any formula involving atan2 in Microsoft Excel, you will need to reverse the arguments, as Excel has them the opposite way around from JavaScript – conventional order is atan2(y, x), but Excel uses atan2(x, y) * For miles, divide km by 1.609344 * For nautical miles, divide km by 1.852 * Thanks to Ed Williams’ Aviation Formulary for many of the formulae " Greg -- 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] Extract printable text from web page using preg_match
I am trying to write a regex function to extract the readable (visible, screen-rendered) portion of any web page. Specifically, I only want the text between the tags, excluding any
Re: [PHP] Extract printable text from web page using preg_match
On 27-Feb-07, at 1:44 PM, Richard Lynch wrote: On Tue, February 27, 2007 11:47 am, M5 wrote: I am trying to write a regex function to extract the readable (visible, screen-rendered) portion of any web page. Specifically, I only want the text between the tags, excluding any
Re: [PHP] Re: Extract printable text from web page using preg_match
On 28-Feb-07, at 1:48 AM, Colin Guthrie wrote: M5 wrote: No, it's not a very good solution. Striptags will leave everything within ,
Re: [PHP] Command-line PHP script CPU usage goes sky-high, stays there--why?
Thanks Jim. Several good points here that I will look into. I've already moved the include() bits into function calls. (That's simple thing I should have corrected long ago.) The socket areas though I'm less sure about how to adjust, since networking programming isn't something I grok naturally. On 10-Dec-07, at 9:57 PM, Jim Lucas wrote: René Fournier wrote: FWIW, here's the stripped-down skeleton of the server: As always, constructive criticism is very welcome. $socket = stream_socket_server("tcp://127.0.0.1:9876", $errno, $errstr); if ($socket) { $master[] = $socket; $read = $master; $write = $master; while (1) { $read = $master; $write = $master; The follow part, I think, is where your problem is. This line tells the system to wait 1 second and then continue, whether you have an inbound connection or not. $mod_fd = stream_select($read, $_w = NULL, $_e = NULL, 1); Then here you are testing for success or failure of the last call. if ($mod_fd === FALSE) { break; } Problem, if you don't have a connection, then the will fail constantly. once every second X (times) the number of connections you have... But my understanding from the docs (where I used one of the examples as a template for the script) socket is that it could/would only fail on startup, that is if it can't perform stream_select() because it's unable to bind to with stream_socket_server(), or am I wrong?: "If the call fails, it will return FALSE and if the optional errno and errstr arguments are present they will be set to indicate the actual system level error that occurred in the system-level socket(), bind(), and listen() calls. If the value returned in errno is 0 and the function returned FALSE, it is an indication that the error occurred before the bind() call. This is most likely due to a problem initializing the socket. Note that the errno and errstr arguments will always be passed by reference." (http://www.php.net/manual/en/ function.stream-socket-server.php) In other wrongs, mod_fd can't return FALSE once the socket_server has been created and bound to the specified IP... right? for ($i = 0; $i < $mod_fd; ++$i) { if ($read[$i] === $socket) {// NEW SOCKET $conn = stream_socket_accept($socket, 900); $master[] = $conn; $key_num = array_search($conn, $master, TRUE); } else { $sock_data = fread($read[$i], 32768); if (strlen($sock_data) === 0) { // CONNECTION GONE $key_to_del = array_search($read [$i], $master, TRUE); fclose($read[$i]); unset ($master[$key_to_del]); } elseif ($sock_data === FALSE) {// CONNECTION BROKEN $key_to_del = array_search ($read[$i], $master, TRUE); fclose($read[$i]); unset($master[$key_to_del]); } else {// READ INCOMING DATA Here, you are not removing the successful connections from $master, so it keeps growing on and on... But only until the connection closes, or no longer blocks (goes away), in which case the program fcloses that socket and removes it from master[]. As for the includes, well, I would turn them into function calls and then have a different function for the different ways you want the program to react. Yes, I did that. It helps a bit with CPU. // include (somefiles); // include (somefiles); // include (somefiles); // [ ... ] } } } } } ?> I didn't see anything about your DB connections. Are those located in the includes? Just at header.inc, once. How long, on average, does your processing of the incoming data take? Because, to me, it looks like you might have a blocking problem with the in-coming connections. If the initial connection takes too long, then the following connections might be getting blocked. You might want to look into pcntl_* functions to do forking if you need it. The processing is pretty quick. I don't think that's a bottleneck. It basically just inserts the data into MySQL, not much processing actually. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re[2]: [PHP] Command-line PHP script CPU usage goes sky-high, stays there--why?
Curiously, would you agree with this guy's comments concerning low- level PHP socket functions vs stream_socket_server() ? "If you want a high speed socket server, use the low-level sockets instead (socket_create/bind/listen). The stream_socket_server version appears to have internal fixed 8k buffers that will overflow if you don't keep up by reading. This is a serious problem if you an application that reads the socket for messages and then, say, saves the result in a database. The delay while it is busy processing means you can't read the data in time unless you get involved in muti-threading. With the the low-level functions, the OS quietly buffers TCP/IP packets so there is no problem (tested on Windows XP Professional)." (http://www.php.net/manual/en/function.stream-socket- server.php#67837) On 10-Dec-07, at 9:46 PM, Tom Rogers wrote: Hi, Tuesday, December 11, 2007, 10:01:38 AM, you wrote: RF> On 10-Dec-07, at 4:42 PM, Tom Rogers wrote: Put a usleep(1000) in the listen while() loop and give the cpu a break. RF> Good advice, but I've already been doing that. The thing is, when the RF> script first starts up, the CPU rarely exceeds 30%, even when many RF> clients (200+) are simultaneously connected and sending data. When a RF> few clients are connected, CPU is typically below 10%. Again, it's RF> only after 24-48 hours that, all of a sudden, CPU usage increases by RF> 40-50%. And it stays high until I stop the script and restart it. RF> One question I have though is, is there actually any benefit to using RF> mysql_pconnect(), since the script simply loops? My understanding is RF> that pconnect only benefits if a script would otherwise be using RF> mysql_connect repeatedly--and this script doesn't, since it calls RF> mysql_[p]connect() just once, in the start tof execution. RF> ...Rene I have found pconnect to be a problem (several years ago) and have never tried it since, it may well be ok now. The most likely cause is memory consumption on long running php scripts, what does top say? I have a script which runs from cron and was hammering the system when it ran and i have had to put the usleep() in the while($result = ..) loop as there are a few thousand rows. Probably bad design but it works and I'm loath to touch it :) One way to solve the memory issue is to have the script started by inetd, slower but more memory friendly. Also have a look at memcached to reduce the load a bit. -- regards, Tom -- 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] MySQL to blame? (was Re: [PHP] Command-line PHP script CPU usage goes sky-high, stays there--why?)
On 20-Dec-07, at 1:17 AM, Per Jessen wrote: René Fournier wrote: I'm really not sure what to try next. ps -aux shows MySQL as hogging the CPU, not PHP or Terminal: When this happens, do a 'SHOW PROCESSLIST' in mysql to see what it's doing. I have, and I can't see anything unusual. There are a few scripts that loop with very slow overhead (with sufficient sleep(), etc.) plus a few outside HTTP requests. Nothing unusual. Incidentally, yesterday, when MySQL went to 80-90% again after a week, I let it stay there while I poked around MySQL (doing the above) and the OS to see if there are some magical limit that I might be breaking. So it the server ran with MySQL at 80-90% CPU for about eight hours. Everything still worked fine, scripts ran, the database was available. That's the thing about this problem--it's not a show- stopper, it's just really strange. And I can't figure its source. After not finding anything, I decided restart the script. So I stop the [seemingly offending] script and wait for CPU load to return to normal. It doesn't. MySQL remains at 80-90%. Even with all the other processes turned off that call MySQL and Web Server off, MySQL remains at 80-90%. Yet SHOW PROCESSES lists no processes, just the "show processlist" command I issue. With the load still high, I attempted to "Stop" MySQL via the Adminstrator control panel. I waited a few minutes. It doesn't shutdown. Finally--and I really hate doing this, because it sees dangerous to data (is it?)--I issue a kill -9 command to its process. Then it starts fine, I start the script in question, and everything is back to normal. ...Rene -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] loadHTML()
Just getting into DOMDocument()... I'm loading an HTML page and trying to extract certain bits of text. Just one problem: loadHTML() seems to ignore orphan tags like ''. For example, in the following HTML: Some text is here. New line. Another new line. Some text is here. New line. Another new line. Some text is here. New line. Another new line. If I run the above HTML through: $nodes = $table->getElementsByTagName("*"); I only get three nodes that I can iterate through (). What I want to do is split/explode the three lines within each div, but when I look at the nodeValue of each node, it only shows something like "Some text is here. New line. Another new line." Any ideas? ...Rene -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] loadHTML()
OK, I already knew that making it valid doesn't change the result. But the question remains, how to parse the HTML as it arrives (which I have no control over anyway), besides doing a str_replace on and inserting a token, which I later replace (which I shouldn't have to, right?) ...Rene On 24-Dec-07, at 7:19 PM, Casey wrote: Actually, never mind. It does not have to be valid to work. On Dec 24, 2007, at 6:15 PM, Casey <[EMAIL PROTECTED]> wrote: That's because it's not proper XHTML: "" should be "". On Dec 24, 2007, at 6:03 PM, M5 <[EMAIL PROTECTED]> wrote: Just getting into DOMDocument()... I'm loading an HTML page and trying to extract certain bits of text. Just one problem: loadHTML () seems to ignore orphan tags like ''. For example, in the following HTML: Some text is here. New line. Another new line. Some text is here. New line. Another new line. Some text is here. New line. Another new line. If I run the above HTML through: $nodes = $table->getElementsByTagName("*"); I only get three nodes that I can iterate through (). What I want to do is split/explode the three lines within each div, but when I look at the nodeValue of each node, it only shows something like "Some text is here. New line. Another new line." Any ideas? ...Rene -- 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] Simple RegEx question
I'm learning regular expressions, and trying to figure out what's possible and what's not. Any ideas of how to create a preg_match expression to parse following three lines: Calgary, AB T2A6C1 Toronto, ON T4M 0B0 Saint John, NBE2L 4L1 ...such that it splits each line into City, Province and Postalcode (irrespective of occasional white space), e.g.: Array ( [city] => "Calgary", [prov] => "AB", [postal]=> "T2A 6C1" ) Array ( [city] => "Toronto", [prov] => "ON", [postal]=> "T4M 0B0" ) Array ( [city] => "Saint John", [prov] => "NB", [postal]=> "E2L 4L1" ) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Problem with GD after upgrading Entropy 5.1.6 -> 5.2.2
I've got a little PHP script that generates charts, that has been working perfectly every day for the past couple years. Since upgrading the Xserve's PHP from 5.1.6 to 5.2.2 (both Entropy), it has stopped working. Specifically, it doesn't return any GIFs (charts). Actually, it will output a GIF providing that session_start() isn't called at the beginning of chart.php. The reason session_start() is called is that the data needed by chart.php resides as session variables—so the session needs to be propogated to chart.php when it's called. My gut feeling is that *something* in chart.php is now throwing a notice or warning or something, and that is corrupting the GIF file (which returns as a broken icon). Here's the beginning of chart.php, which as I mentioned, was working perfectly verbatim up until last week: Here is a modified version of the above, based on comments I've gleaned from various forums. (It still doesn't work, btw.)
Re: [PHP] Problem with GD after upgrading Entropy 5.1.6 -> 5.2.2
Thanks Jim, Such common sense revealed the cause—out of memory when generating the chart. The session 'problem' was a red herring—the reason the script ran out of memory is that GD only needed more than 8MB when it was able to produce a large enough GIF (since the session data was available). Upgrading to 5.2.2 reset the max_memory to 8M (which I didn't think it did, but there you go). Increasing it to 16MB solves the issue. Thanks again. ...Rene On 10-Jul-07, at 3:09 PM, Jim Lucas wrote: M5 wrote: I've got a little PHP script that generates charts, that has been working perfectly every day for the past couple years. Since upgrading the Xserve's PHP from 5.1.6 to 5.2.2 (both Entropy), it has stopped working. Specifically, it doesn't return any GIFs (charts). Actually, it will output a GIF providing that session_start() isn't called at the beginning of chart.php. The reason session_start() is called is that the data needed by chart.php resides as session variables—so the session needs to be propogated to chart.php when it's called. My gut feeling is that *something* in chart.php is now throwing a notice or warning or something, and that is corrupting the GIF file (which returns as a broken icon). Here's the beginning of chart.php, which as I mentioned, was working perfectly verbatim up until last week: Here is a modified version of the above, based on comments I've gleaned from various forums. (It still doesn't work, btw.) Maybe see if display_errors is on, if so, ini_set('display_errors', 0); might help but what you might want to do is turn on everything for error reporting and view the page with the gif output part disabled and see what it says. now browse to the page and see what it says. Make sure you comment out the header part that sends a custom Content-type also. -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php