align-toparrow-leftarrow-rightbackbellblockcalendarcamerachatcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-crosscrosseditfacebookglobegoogleimagesinstagramlocation-pinmagnifying-glassmailmoremuplabelShape 3 + Rectangle 1outlookpersonplusImported LayersImported LayersImported Layersshieldstartwitteryahoo

Re: [php-360] fputs og while/feof

From: Jacob O.
Sent on: Saturday, February 2, 2013 3:23 PM
I would think that the one that takes a long time is the call that ends up timing out because the response does not contain any more data. It is up to you, as a http 1.1 client to know that you have received a complete http response when you have received 2 bytes of data (you can know that from the content-length) and then an extra linebreak (as I recall) indicating the end of the message. Once you have received the full response you can terminate the connection. The http 1.1 server wont let you know in any other way that the message is over. Which is why the socket functions in php have no way of knowing that the message is over and is therefore simply waiting for more data.

My point was that it is probably not a good idea to attempt to parse the http response your self. As mentioned, the http 1.1 protocol gets to complex. I think, in principle, you would have to worry about alternate transfer-encodings for instance. 

It would be better just to use some of the http capabilities of php instead of constructing your own http post request, as I expect you do in the code you have omitted. I would go with file_put_contents if you are certain that you can have the entire message you are posting in memory (in a $variable). 

On Feb 2, 2013, at 15:32 , Jette Derriche <[address removed]> wrote:

> Hi Jacob and Travis
> 
> Thanks a lot for your suggestions. You may be right, but I just don't think it makes sense that every loop in the "while" takes just miliseconds, apart from the one that fetches the blank line - it stalls for 10 seconds.
> 
> My first thought was that maybe the script at the other end was very slow, but if that was the case, my second code example wouldn't complete that fast.
> 
> But you are probably right, that it is better to rely on the first line, rather than the script output at the end.
> 
> /Jette
> 
> On 02/02/[masked]:38 AM, Jacob Oettinger wrote:
>> Hi again
>> 
>> Thinking about it, the easiest might be using file_put_contents to do
>> the upload. That is built into standard php and does not require any
>> http stuff from you. There is an example of how to do it here:
>> 
>> http://stackoverf...­
>> 
>> You might have to do some googling to figure out how to post files.
>> 
>> Jacob
>> 
>> On Feb 2, 2013, at 9:32 , Jacob Oettinger <[address removed]
>> <mailto:[address removed]>> wrote:
>> 
>>> Hi
>>> 
>>> I am fairly certain Travis' answer is correct. As you can see in the
>>> response nginx sends connection-keep: alive which means that it
>>> suggests keeping the connection open for following requests, which in
>>> turn means that it will not send eof. So to know if your request
>>> succeeded you need to parse the http response (which is not as simple
>>> as it sounds, http, especially 1.1, is rather complex once you get
>>> into it). But in your case it might be ok to just read the first line
>>> of the response and if that is "OK" assume that everything went well
>>> and close the connection, without parsing the http response correctly.
>>> 
>>> Alternatively you could make sure the transaction uses http 1.0 which
>>> does not allow connection: keep-alive and would cause the responding
>>> server to close the connection upon completion of the response.
>>> 
>>> Finally you could use some library that does handling of http for you.
>>> Something like the curl extension, PEAR Http or Zend_Http to name a few.
>>> 
>>> Jacob
>>> 
>>> 
>>> On Feb 1, 2013, at 16:53 , Travis Elkins <[address removed]
>>> <mailto:[address removed]>> wrote:
>>> 
>>>> I'm not 100% certain myself, but it looks like it might have less to
>>>> do with the while statement and more to do with the feof() call.
>>>> 
>>>> Check out the first warning in the notes here:
>>>> http://www.php.ne...­.  It applies to feof()
>>>> when fsockopen() is used, which is the case in the code that you shared.
>>>> 
>>>> Perhaps the connection wasn't closed by the server, so it's timing
>>>> out.  And, 10 seconds is certainly a value that someone might set
>>>> default_socket_timeo­ut to...although the default is 60
>>>> (http://www.php.ne...­).
>>>> 
>>>> Hope that helps...or I hope it's at least in the right ballpark.
>>>> 
>>>> 
>>>> 2013/1/31 Jette Derriche <[address removed] <mailto:[address removed]>>
>>>> 
>>>>    Hej alle
>>>> 
>>>>    God jul, godt nytår og håber I holder varmen - Jeg er tilbage
>>>>    efter ½ års barsel. Det er godt at komme til tastaturet igen :-)
>>>> 
>>>>    Jeg har siddet i dag og kæmpet med en funktion der post'er filer
>>>>    til en anden server. Det tog 10 sekunder at overføre en lille
>>>>    billedfil på 146kb, og så lang tid skal det jo slet ikke tage.
>>>> 
>>>>    Her er koden i forsimplet udgave:
>>>> 
>>>> 
>>>>    --------------------­----------__--------­--------------------­--__----------
>>>>    if (!$fp = fsockopen($someserve­r,80,$__errno, $errstr,5))
>>>>      die('Connection failure');
>>>>    fputs($fp, $data);
>>>>    while (!feof($fp)) echo fgets($fp,32000);
>>>>    fclose($fp);
>>>>    --------------------­----------__--------­--------------------­--__----------
>>>> 
>>>> 
>>>>    Her er eksempel output:
>>>> 
>>>> 
>>>>    --------------------­----------__--------­--------------------­--__----------
>>>>    HTTP/[masked] OK
>>>>    Server: nginx/1.2.4
>>>>    Date: Thu, 31 Jan[masked]:12:45 GMT
>>>>    Content-Type: text/html
>>>>    Content-Length: 2
>>>>    Connection: keep-alive
>>>>    X-Powered-By: PHP/5.4.6
>>>> 
>>>>    OK
>>>>    --------------------­----------__--------­--------------------­--__----------
>>>> 
>>>> 
>>>>    Det underlige er, at de første 7 linier kommer med få
>>>>    milisekunders mellemrum. Den loop der henter den blanke linie
>>>>    tager 10 sekunder, og derefter kommer så den sidste "OK" som er
>>>>    output fra det script der modtager filen.
>>>> 
>>>>    Nu har jeg ændret koden til følgende:
>>>> 
>>>> 
>>>>    --------------------­----------__--------­--------------------­--__----------
>>>>    if (!$fp = fsockopen($someserve­r,80,$__errno, $errstr,5))
>>>>      die('Connection failure');
>>>>    fputs($fp, $data);
>>>>    echo fread($fp,32000);
>>>>    fclose($fp);
>>>>    --------------------­----------__--------­--------------------­--__----------
>>>> 
>>>> 
>>>>    Nu får jeg hele output uden forsinkelser...
>>>> 
>>>>    Jeg har aldrig oplevet dette før, og faktisk har jeg på samme
>>>>    website et andet stykke php kode der også bruger while metoden
>>>>    uden problemer.
>>>> 
>>>>    på php.net <http://php.net/&g...;­ under fsockopen(), viser de netop
>>>>    while/feof metoden som eksempel. Hvad hulan er det jeg misser her?
>>>> 
>>>>    Mvh. Jette
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>>    --
>>>>    Please Note: If you hit "REPLY", your message will be sent to
>>>>    everyone on this mailing list ([address removed]
>>>>    <mailto:[address removed]>)
>>>>    http://www.meetup...­
>>>>    <http://www.meetup...;­
>>>>    This message was sent by Jette Derriche ([address removed]
>>>>    <mailto:[address removed]>) from The Copenhagen PHP Meetup Group.
>>>>    To learn more about Jette Derriche, visit his/her member profile:
>>>>    http://www.meetup...­
>>>>    <http://www.meetup...;­
>>>>    Set my mailing list to email me
>>>> 
>>>>    As they are sent
>>>>    http://www.meetup...­
>>>>    <http://www.meetup...;­
>>>> 
>>>>    In one daily email
>>>>    http://www.meetup...­
>>>>    <http://www.meetup...;­
>>>> 
>>>>    Don't send me mailing list messages
>>>>    http://www.meetup...­
>>>>    <http://www.meetup...;­
>>>>    Meetup, POB 4668 #37895 NY NY USA 10163 | [address removed]
>>>>    <mailto:[address removed]>
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> --
>>>> Please Note: If you hit "*REPLY*", your message will be sent to
>>>> *everyone* on this mailing list ([address removed]
>>>> <mailto:[address removed]>)
>>>> This message was sent by Travis Elkins ([address removed]
>>>> <mailto:[address removed]>) from The Copenhagen PHP Meetup
>>>> Group <http://www.meetup...;­.
>>>> To learn more about Travis Elkins, visit his/her member profile
>>>> <http://www.meetup...;­
>>>> Set my mailing list to email me As they are sent
>>>> <http://www.meetup...;­ | In one
>>>> daily email <http://www.meetup...;­
>>>> | Don't send me mailing list messages
>>>> <http://www.meetup...;­
>>>> 
>>>> Meetup, POB 4668 #37895 NY NY USA 10163 <x-msg://3426/#>­ |
>>>> [address removed] <mailto:[address removed]>
>>> 
>>> 
>>> 
>>> 
>>> 
>>> --
>>> Please Note: If you hit "*REPLY*", your message will be sent to
>>> *everyone* on this mailing list ([address removed]
>>> <mailto:[address removed]>)
>>> This message was sent by Jacob Oettinger ([address removed]
>>> <mailto:[address removed]>) from The Copenhagen PHP Meetup Group
>>> <http://www.meetup...;­.
>>> To learn more about Jacob Oettinger, visit his/her member profile
>>> <http://www.meetup...;­
>>> Set my mailing list to email me As they are sent
>>> <http://www.meetup...;­ | In one
>>> daily email <http://www.meetup...;­ |
>>> Don't send me mailing list messages
>>> <http://www.meetup...;­
>>> 
>>> Meetup, POB 4668 #37895 NY NY USA 10163 <x-msg://3437/#>­ |
>>> [address removed] <mailto:[address removed]>
>> 
>> 
>> 
>> 
>> 
>> --
>> Please Note: If you hit "*REPLY*", your message will be sent to
>> *everyone* on this mailing list ([address removed]
>> <mailto:[address removed]>)
>> This message was sent by Jacob Oettinger ([address removed]) from The
>> Copenhagen PHP Meetup Group <http://www.meetup...;­.
>> To learn more about Jacob Oettinger, visit his/her member profile
>> <http://www.meetup...;­
>> Set my mailing list to email me As they are sent
>> <http://www.meetup...;­ | In one daily
>> email <http://www.meetup...;­ | Don't
>> send me mailing list messages
>> <http://www.meetup...;­
>> 
>> Meetup, POB 4668 #37895 NY NY USA 10163 <#> | [address removed]
> 
> 
> 
> 
> --
> Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
> http://www.meetup...­
> This message was sent by Jette Derriche ([address removed]) from The Copenhagen PHP Meetup Group.
> To learn more about Jette Derriche, visit his/her member profile: http://www.meetup...­
> Set my mailing list to email me
> 
> As they are sent
> http://www.meetup...­
> 
> In one daily email
> http://www.meetup...­
> 
> Don't send me mailing list messages
> http://www.meetup...­
> Meetup, POB 4668 #37895 NY NY USA 10163 | [address removed]
> 

Our Sponsors

  • Eye4web

    Lokation + drinks & snacks

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy