Wednesday, October 6, 2010

file_get_contents acting very slow

Lately i have been working on a simple REST-like service interface to an existing product at work. I admit, I found some code online that I modified. No need to do work already done, right? ;-)

The problem arose when i tried to call my brand new web service. I got extremely long waiting time (about 15 secs to precise), before the resource was returned. The code I used look like this:

$result = file_get_contents('http://example.com/service/');

As simple as it gets. When I tried calling the service directly from my browser, the result came back instantly.

I turned to my old friend Google to look for an answer. Most of the answers i got was that the DNS lookup was slow. So i tried my script from all the different servers I have access to, but with the same result.

The solution was quite simple, when I finally found it. In my REST-like service I set the follow header:

header('HTTP/1.1 200 OK');

By doing this I force the connection to use HTTP 1.1. This is pretty standard, but apparently by doing this I use the default behaviour of HTTP 1.1 and that is to keep the connection alive. The same as setting:

header('Connection: keep-alive');

So the connection started by file_get_contents() is keept open, until it times out. Timeout is set to 15 secs on the server!! So the solution was to put the following header in my service:

header('Connection: close');

A w00p w00p the result was returned instantly.

A quick tip if you run into this problem, and the service is not your own, is to set the header yourself. You can do this by giving a context to the file_get_contents. This can be done by doing the following:

$opts = array(

'http'=>array(
header' => 'Connection: close'
)
);
$context = stream_context_create($opts);

$result = file_get_contents('http://example.com/service/', false, $context);

This will force the connection to close immediately after the resource have been retrived. Al option that can be set in a context (for HTTP) can be found here: http://php.net/manual/en/context.http.php

Monday, March 8, 2010

POST array in PHP decoded wrong??

I just stubled upon an error? in the way PHP decode the keys in the $_POST array. If you define an input field in a form with a name containing a . (period) f.x. 'test.field', then the corresponding key in the $_POST array, when posted to a PHP script, will be 'test_field'. PHP substitute the . with a _ (underscore). According the HTML spec the . is a legal character in the name as long as it starts with [a-zA-Z].
So why do PHP make this substitution? I tried to find the answer online, but without any luck. Please post a comment if you know the answer or where to find it.

Update
I found this comment on php.net http://www.php.net/manual/en/language.variables.external.php#81080 Apparently this is done for backwards compatibility with register globals.

Thursday, December 10, 2009

Happy camper qwerty-keyboard in danish

Finally I got a danish keyboard on my HTC Magic.

I bought my Magic a few month ago and I must say "I love it" (quote Ole H.) But since day 1 a danish qwerty-keyboard has been missing. About a month ago a lost my patients with HTC and unlocked my phone and installed a modded version of Android 1.6. I too want the new features. Btw. the Tattoo and Hero are ugly, so no I will not buy one of them. Well back to the original subject. My new and improved Magic with A 1.6 was rolling, YIR! But still no danish qwerty-keyboard and now I don't even have my danish dictionary any more, crap. But to day i found to obvious solution, Android Marked. A kind soul told me that I could just search the Android Marked for a danish qwerty-board and a danish dictionary. Search search search.... PLING... Keyboard and dictionary found and dl in 30 sec. Quick setup of the keyboard and now I'm rolling wind texting i danish.

Thank you Android Marked

So a word of advise for the next time for you and me, go to the marked

Friday, November 27, 2009

php, LDAP and the whole tree

After using several hours on trying to search the entire AD tree on Windows 2003, I finally found the solution. Copied directly from php.net:

allie at lsu dot edu
06-Mar-2007 10:23
I sure do wish there was some way I could get this information out to all programmers in the world about binding and searching MS AD. This is the second time I was bit by the "I need to search the entire tree" problem.

For php (and apache auth_ldap ) you need to specify port 3268 when you want to search the entire tree. Otherwise it will spit out the partial results error.

ldap_connect($server,3268);

I'm just fortunate enough to have won this same battle with apache searching the whole directory. When I noticed our php application failing auth's for users, I was immediately able to fix the problem by adding this port specification (and the ldap_set_option($ldapserver, LDAP_OPT_REFERRALS, 0) option).

I really hope this helps someone else before they pull all their hair out. I know I miss mine.

Tuesday, May 26, 2009

Glemte HTML tags

Faldt over denne artikel, som beskriver 10 tags, som man burde bruge i stedet for at misbruge DIV tags.