fsockopen() and feof() = bleh!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chrisman826
Forum Newbie
Posts: 1
Joined: Fri Dec 01, 2006 3:12 am

fsockopen() and feof() = bleh!

Post by chrisman826 »

hello. thanks for the membership all of you moderators.

this is the first thing I have ever programmed that has actually caused me to look for help on a forum. but i just cannot figure this thing out.

I am using fsocketopen and feof to post variables to another page, in which that other page does some db querying. I am doing it this way because it is cross domain. im using it with wordpress fyi. it connects to WP and uses the wp functions to create a new post remotely for me. here is the code

Code: Select all

<?php

    //open remote posting script
    //set the headers
       // these vars are set from a db query
    $req = 'title='. $title . '&content=' . $content . '&category=' . $category . '&logon=' . $logon . '&pass=' . $pass;
    $header .= "POST /remote_post.php HTTP/1.0\r\n";
    $header .= "Host: http://www.example.com\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: " . strlen ($req) . "\r\n";
    $header .= "Connection: Close\r\n\r\n";
    $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
    
    $SUCCESS = false;
    
    //check the connection
    if (!$fp) {
      $status_message = "$errstr ($errno)";
      $res = "FAILED";
    }
    // if connect ok write the POST request variables
    else {
      fputs ($fp, $header . $req);
      //read the results for a "SUCCESS" -ful DB insert
      while (!feof($fp) && $SUCCESS==false) {
        $res = fgets ($fp, 1024);
        if (strcmp ($res, "SUCCESS") == 0) {
          $SUCCESS = true;
        }
        if(!empty($res)){
          $last_line = $res;
        }
        
      }
    }
    fclose($fp);

?>
the remote script takes the posted variables, inserts them into the WP db with WP functions and then echo SUCCESS or FAIL -> for the $res var to catch.
Now, this script *works* like it is supposed to. it just takes FOREVER.

i think maybe that feof() is not returning true even at eof - and so it continues looping until the connection times out at like a minute or something like that. I think this because while its still loading i can check my WP and everything is already posted - which means that technically the "SUCCESS" should already be echoed in the remote_post.php and eof should then occur. maybe not???

anywho. thats when i tried adding the $SUCCESS = false to the while demands ie ( while(!feof($fp) && $SUCCESS==false)) but that did not work. it still took FOREVER. I have tried numerous things from adding an extra fgets(). i just cant figure this one out.
maybe ill just try curl and see what happens.... any and all help is appreciated.
Post Reply