Page 1 of 1

PHP & Sending post data VIA fsockopen()... Problems...

Posted: Wed Jun 04, 2003 12:59 pm
by Sky
I've been working on a overlay for a old UBB board, and I'm trying to post to it from a php script I wrote. Here are the functions...

The script is not returning any errors, nor can I see any, but it really isn't doing or returning ANYTHING. the target forum for this experiment is http://www.cognitoy.com/cgi-bin/forumdi ... s&number=3

I built the request by looking over the new topic form. Got the IP by trying to D/L a page with FlashGet.

I get no response from the script at all.

Code: Select all

<?php
function send_headers ($fp) {
	fputs ($fp, "Accept: */*\n");
	fputs ($fp, "Accept-Language: en\n");
	fputs ($fp, "Connection: Keep-Alive\n");
	fputs ($fp, "User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)\n");
}

// post data and return reply
function post_data ($host, $url, $data) {
	$fp = fsockopen ($host, 80, $errno, $errstr, 120) or die($errno.$errstr);
	$ret = "";
		if (strncasecmp ($url, "http://", 7) == 0) $url = substr ($url, 7);
	$p = strpos ($url, '/');
	if (empty ($p)) {
        $req = "/";
    } else {
        $req = substr ($url, $p);
    }
    if ($fp) {
		fputs ($fp, "POST $req HTTP/1.0\n");
		send_headers ($fp);
		fputs ($fp, "Content-type: application/x-www-form-urlencoded\n");
		$out = "";
		while (list ($k, $v) = each ($data)) {
			if(strlen($out) != 0) $out .= "&";
			$out .= rawurlencode($k). "=" .rawurlencode($v);
		}
		$out = trim ($out);
		fputs ($fp, "Content-length: ".strlen($out)."\n\n");
		fputs ($fp, "$out");
		fputs ($fp, "\n");
		while(!feof($fp)){
            $ret .= fgets($fp,128);
        }
	fclose ($fp);
	}
	return $ret;
}

function mrbb_make_topic($forum, $user, $pass, $topic, $msg) {
  $forum_number = array(
    'New Player Discussion' => 1,
    'Advanced Player Discussion' => 2,
    'Questions/Comments' => 3,
    'Bug Report Forum' => 4,
    'New Component Ideas' => 5,
    'New Scenario Ideas' => 6,
    'Contests' => 8,
    'ICE SDK' => 9,
    'MindRover and Robotics' => 12,
    '5 Card Dash' => 13
  );
  $forumn = $forum_number{$forum};
  $post = array(
    'action'      => 'posttopic',
    'forum'       => $forum,
    'number'       => $forumn,
    'UserName'     => $user,
    'Password'    => $pass,
    'MsgIcon'      => 1,
    'Message' => $msg,
    'TopicSubject' => $subject,
    'Signature'   => 'yes'
  );
  $ret = post_data("66.70.176.91", "/cgi-bin/postings.cgi", $post);
  return $ret;
}

function mrbb_make_topic2() {
  $tatm = "This is Sky. Hi all (Sorry for any annoyances, Kent/Nat).
This has been posted from my app, on my pc. Interface isn't made yet, but we'll see...
--Sky";
  mrbb_make_topic('Questions/Comments', $user, $pass, 'TepApp Test', $tatm);
}
?>
As you might have noticed, this is actually run with php-gtk (I'm having lots of success with it...), latest version of php. mrbb_make_topic() is called via a button currently. I know the code executes, becase it required a little debugging to get no error responses O.o Please help ASAP...

Posted: Wed Jun 04, 2003 1:09 pm
by Sky
DOH!!!! Will someone move this to the php forum? pweeze?

Posted: Wed Jun 04, 2003 1:25 pm
by twigletmac
Sky wrote:DOH!!!! Will someone move this to the php forum? pweeze?
Done

Mac

Posted: Wed Jun 04, 2003 1:54 pm
by hedge
just took a quick look. It seems to me that you are using a \n\n between headers where it should be \r\n and \r\n\r\n between headers and content.

This is a link to some code where I am doing something similar (at the bottom). You may or may not find it useful.

viewtopic.php?t=7063

Posted: Wed Jun 04, 2003 3:45 pm
by Sky
See, I didn't write the send code, I just got it... I had no idea O.o

Posted: Wed Jun 04, 2003 4:49 pm
by Sky
Ok, this is what i'm getting back... (It's still not posting)

Code: Select all

HTTP/1.1 200 OK
Date: Wed, 04 Jun 2003 21:43:23 GMT
Server: Apache/1.3.14 (Unix) mod_perl/1.24 PHP/4.0.3pl1 FrontPage/4.0.4.3 mod_ssl/2.7.1 OpenSSL/0.9.6
Connection: close
Content-Type: text/html
(Note, this version (\n not \r\n, i thought that was the way it goes for *nix?) returns the same as your \r\n version.)

This is the modified function:

Code: Select all

<?php
function httpSimulateFormSubmit($host, $method, $path, $data) {
   $fp = @fsockopen($host, 80);
   if ($fp) {
     if ($method == 'GET') $path .= '?' . $data;
  		while (list ($k, $v) = each ($data)) {
  			if(strlen($data) != 0) $out .= "&";
  			$data2 .= $k ."=" .$v;
  		}
     fputs($fp, "$method $path HTTP/1.0\n");
     fputs($fp, "Host: $host\n");
     fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
     fputs($fp, "Content-length: " . strlen($data2) . "\n");
     fputs($fp, "Connection: close\n\n");
     if ($method == 'POST') fputs($fp, $data2);
     while (!feof($fp)) $buf .= fgets($fp, 1024);
     fclose($fp);
     return $buf;
   }
   else return false;
}
?>
And this is the call code:

Code: Select all

<?php
function mrbb_make_topic($forum, $user, $pass, $topic, $msg) {
  $forum_number = array([.....]);
  $forumn = $forum_number{$forum};
  $post = array(
    'action'      => 'posttopic',
    'forum'       => &$forum,
    'number'       => &$forumn,
    'UserName'     => &$user,
    'Password'    => &$pass,
    'MsgIcon'      => 1,
    'Message' => &$msg,
    'TopicSubject' => &$subject,
    'Signature'   => 'yes'
  );
  $ret = httpSimulateFormSubmit("66.70.176.91", "POST", "/cgi-bin/postings.cgi", &$post);
  return $ret;
}

function mrbb_make_topic2() {
  global $user, $pass;
  $tatm = "[....]";
  echo mrbb_make_topic('Questions/Comments', $user, $pass, 'TepApp Test', &$tatm);
}
?>

Posted: Wed Jun 04, 2003 5:45 pm
by Sky
Ok, here's a version with one bug (not adding &'s to url-encoded) fixed, and it's output: (BTW, the site comes up with a error "We cannot post your topic because it does not contain at least one letter or number.", so apparently there's a error in the message part..)

Code: Select all

<?php
function httpSimulateFormSubmit($host, $method, $path, $data) {
   $fp = @fsockopen($host, 80);
   if ($fp) {
     if ($method == 'GET') $path .= '?' . $data;
      unset($showand);
  		while (list ($k, $v) = each ($data)) {
  			if(strlen($data) != 0) $out .= "&";
  			$data2 .= $showand.rawurlencode($k) ."=" .rawurlencode($v);
  			$showand = "&";
  		}
     fputs($fp, "$method $path HTTP/1.0\n");
      echo "$method $path HTTP/1.0\n";
     fputs($fp, "Host: $host\n");
      echo "Host: $host\n";
     fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
      echo "Content-type: application/x-www-form-urlencoded\n";
     fputs($fp, "Content-length: " . strlen($data2) . "\n");
      echo "Content-length: " . strlen($data2) . "\n";
     fputs($fp, "Connection: close\n\n");
      echo "Connection: close\n\n";
     if ($method == 'POST') fputs($fp, $data2."%20 \n");
      print_r($data2);
     while (!feof($fp)) $buf .= fgets($fp, 1024);
     fclose($fp);
     return $buf;
   }
   else return false;
}
?>

Code: Select all

POST /cgi-bin/postings.cgi HTTP/1.0
Host: 66.70.176.91
Content-type: application/x-www-form-urlencoded
Content-length: 355
Connection: close

action=posttopic&amp;forum=Questions%2FComments&amp;number=3&amp;UserName=Altair&amp;Password=****&amp;MsgIcon=1&amp;Message=This%20is%20Sky.%20Hi%20all%20%28Sorry%20for%20any%20annoyances%2C%20Kent%2FNat%29.%0D%0AThis%20has%20been%20posted%20from%20my%20app%2C%20on%20my%20pc.%20Interface%20isn%27t%20made%20yet%2C%20but%20we%27ll%20see...%0D%0A--Sky&amp;TopicSubject=&amp;Signature=yesHTTP/1.1 200 OK
Date: Wed, 04 Jun 2003 22:42:28 GMT
Server: Apache/1.3.14 (Unix) mod_perl/1.24 PHP/4.0.3pl1 FrontPage/4.0.4.3 mod_ssl/2.7.1 OpenSSL/0.9.6
Connection: close
Content-Type: text/html

Posted: Thu Jun 05, 2003 9:08 am
by hedge
Not sure if that's a question or not, but to see what the other server returns to you you can do the following: (assuming using my functions)

Code: Select all

httpParseResponse($rslt, $hdr, $data);
header($hdr);
echo $data;
BTW the \r\n has nothing to do with unix, that's what's specified in the http protocol. It may work with a \n but you may not be able to split the headers from the content reliably... that's what the httpParseResponse function does.

Posted: Thu Jun 05, 2003 11:04 am
by Sky
The response is right after the post request... echo'ed as sent/recieved. It is apparently getting some of the data, like password, or action... but not the 'Message' field!

Oh, and I tried your function, I even tried a hugo-mundo class I found (snoopy) and none work. I'm thinking I may be leaving something out, or it's just not going to work.

any suggestions, anyone?