UPDATE:
In case anyone else runs into this issue (having to add oauth to image requests) it turned out to be a call to session_start() that was doing the blocking. Since I only needed to read from the session (get session id to lookup auth token) I added a call to session_write_close() which immediately removed the blocking and everything is speedy again.
Original Question:
I have an app that loads and displays many images from a remote site. When I link to those images as follows, it is of course blazing fast.
<img src="url-to-remote-image" />
However, the remote site is going to require oauth on the images soon. So in prep, I wrote a small php script to add the oauth, fetch the image and return to the calling browser. Unfortunately this seems a LOT slower (even taking oauth out of the equation). Following is the source for fetchimage.php (note, I have stripped out everything including oauth to simplify the example).
<?php
$imageUrl = $_GET["path"];
$options = array(
'http' => array(
'method' => 'GET'
)
);
$context = stream_context_create($options);
$result = file_get_contents($imageUrl, false, $context);
echo $result;
exit;
And of course I modified my img tags to look like this:
<img src="https://myserver/fetchimage.pgp?path=url-to-remote-image" />
This works and fetches the images, but it is up to 100 times slower! I even tried curl instead of file_get_contents but same issue. I have a feeling it is php limiting the number of requests it can process at a time.
I did this same test completely contained to my local system. With the img tag pointing to local files it took 80ms to load the 20 test images. When the img tag pointed to fetchimage.php on the local machine and changing the fetch url to local (instead of remote) the same test took 8 seconds. When I profiled it I see that all the time seemed to be in waiting. Out of the 20 images it would fetch 6 at once, they all took around the same time, and all other requests were stalled until the first 6 completed. At that point 6 more started to get fetched. The actual calls to file_get_contents seemed to be as fast as what I was seeing the straight image linking in the img tag.
I see similar results when calling to the remote systems so I know it isn't just my local environment. Why can apache server those 20 images in 80ms but as soon as php is asked to do the same it takes 100 times longer? Any pointers would be greatly appreciated as I feel I have hit a wall.