Need response with fputs and fget.

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
jason0000
Forum Newbie
Posts: 4
Joined: Wed Oct 09, 2002 4:04 am

Need response with fputs and fget.

Post by jason0000 »

Hi everyone,

I am troubled! What the below snippet is meant to do is send a bunch of data using the socket connection then receive a response to that data and print it out. It does not give an error; instead it just loads and loads until I believe it times out then just loads a blank html file. Also the site it is connecting too is a remote secure server, prepared to handle scripts sending the appropriate data.

Snippet:

$data = "variable=1";
$host = "www.mysite.com";
$method = "POST";
$path = "/cgi-bin/gatway.cgi";
$fp = fsockopen($host,80);
fputs($fp, "$method $path\n");
fputs($fp, "Host: $host\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
fputs($fp, "Content-length: " . strlen($data) . "\n");
$result = fgets($fp);
fclose($fp);
list(variable1,$variable2) = split ("|", $result);
echo "$variable1,$variable2";


To try and explain what I mean, this is a Windows NT/2000 Perl 5.6 – IE 5.01 Object Method example snippet that does work:

use WIN32::OLE;
$SendObject=Win32::OLE->new('microsoft.XMLhttp');
$SendObject->open("POST", "https://www.mysite.com/cgi-bin/gateway.cgi", "false");
$SendObject->setRequestHeader("Content-type", "text/plain");
$SendObject->send ($Postvals);
$Result = $SendObject->responseText;
($variable1,$variable2) = split(/\|/, $Result);

Here is a Unix/Windows NT/2000 Perl 5.6 – Crypt Module Installed example snippet that works:

use LWP::UserAgent;
use Crypt::SSLeay;
$ua = new LWP::UserAgent;
$ua->agent("SSL/0.1");
my $req = new HTTP::Request('POST', 'https://www.mysite.com/cgi-bin/gateway.cgi');
$req->content_type('application/x-www-form-urlencoded');
$req->content($Postvals);
my $res = $ua->request($req);
$Result = $res->content;
($variable1,$variable2) = split(/\|/, $Result);


Here is a Windows NT/2000 VBScript ASP Object Method example snippet:

Set SendObject = Server.CreateObject("Microsoft.XMLhttp")
SendObject.open "POST","https://www.mysite.com/cgi-bin/gateway.cgi",false
SendObject.setRequestHeader "Content-type", "text/plain"
SendObject.send Postvals
Result = SendObject.responseText
SplitArray = split(Result, "|", -1, 1)
success = SplitArray(0)
authcode = SplitArray(1)
authresponse = SplitArray(2)
avscode = SplitArray(3)
oid = SplitArray(4)

All of the examples work fine but I cannot use them because I am running a Linux server with Apache and PHP installed without the capabilities.

I figured there must be something that I am missing and I would be very grateful for any help!
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

I'm not 100% sure that I'm following you, but it sounds a lot like you are talking about a client application of some sort. That said, if you check out the manual at php.net/socket or
http://www.php.net/manual/en/ref.sockets.php, you should also see some examples of
servers and clients. If you are doing what I think, you could prolly extend or change to suit your needs the client example on the page.

Cheers,
BDKR
jason0000
Forum Newbie
Posts: 4
Joined: Wed Oct 09, 2002 4:04 am

Post by jason0000 »

Thanks for the reply! I think I made this sound more complicated then it is. What I meant to say is that I need the script to connect via the socket connection then transfer data through the post method and wait till it receives a response back and print out that response. I have extensively searched through the php site but could not find information on how to do this. I will check more through the link that you provided. Thanks again!
jason0000
Forum Newbie
Posts: 4
Joined: Wed Oct 09, 2002 4:04 am

Post by jason0000 »

Just checked out the link and it appears to be exactly what I am looking for. Here is one example given:

<?php
error_reporting (E_ALL);

echo "<h2>TCP/IP Connection</h2>\n";

/* Get the port for the WWW service. */
$service_port = getservbyname ('www', 'tcp');

/* Get the IP address for the target host. */
$address = gethostbyname ('www.example.com');

/* Create a TCP/IP socket. */
$socket = socket_create (AF_INET, SOCK_STREAM, 0);
if ($socket < 0) {
echo "socket_create() failed: reason: " . socket_strerror ($socket) . "\n";
} else {
echo "OK.\n";
}

echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect ($socket, $address, $service_port);
if ($result < 0) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
} else {
echo "OK.\n";
}

$in = "HEAD / HTTP/1.0\r\n\r\n";
$out = '';

echo "Sending HTTP HEAD request...";
socket_write ($socket, $in, strlen ($in));
echo "OK.\n";

echo "Reading response:\n\n";
while ($out = socket_read ($socket, 2048)) {
echo $out;
}

echo "Closing socket...";
socket_close ($socket);
echo "OK.\n\n";
?>


I will attempt to try it out!
Post Reply