Http Posts

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

timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

In essence, the question would be: Does your ISP allow you (the useraccount that executes the php script) to make an outgoing connection to (server, port)?
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

In essence, the question would be: Does your ISP allow you (the useraccount that executes the php script) to make an outgoing connection to (server, port)?
I already stated that I can not use the port on my server to connect to a remote server, thus I can not use cURL methods. My only option is to make a post request.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Are you sure about this? Because even with fskopen you need a local port to be opened ... That is the way Internet works ;)

Most of the hosting providers would refuse new connections *TO* their servers (except their service ports), but would allow outgoing connection and the incoming packets which are realated to this connection.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

VladSun wrote:Are you sure about this? Because even with fskopen you need a local port to be opened ... That is the way Internet works ;)
I don't mean to argue with you but it's a bit confusing. Didn't you say "Generally - no, you don't need." in response to my post prior? And doesn't "Generally - no, you don't need." mean I don't need to open a port, but now you say I do? Or is it just a misunderstanding? You thought that the port of mine is already open and I don't need to open it so you said "Generally - no, you don't need.", rather then what I really meant which is that my port isn't open and I'm not authorized to open it. :?

And if all posts to a server need to open a port in order to interact than who's port is being opened when ajax or an html form makes a request? Is it the clients ports that are both connecting? And more importantly, which port umber is being used, to be specific? The usual port 80 is fine, it's just port number 1129 that I'm having trouble with.

Also is a socket just a connection between two ports? :D

Okay I think I'm getting sidetracked on my original question.

So can someone explain to me what this script does line-by-line:

Code: Select all

// Request
$request = "POST /path HTTP/1.0\r\n"
         . "Host: domain.tld\r\n"
         . "Content-Type: application/x-www-form-urlencoded;\r\n"
         . "Content-Length: " . strlen($dataToPost) . "\r\n"
         . "\r\n"
         . $dataToPost;
$fs = @fsockopen($host, $port, $errno, $errstr, 10);
fwrite($fs, $request);

// Response
$response = '';
while (!feof($fs)) {
    $response .= fgets($fs, 1160);
}
fclose($fs);
Please. :D
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

JellyFish wrote:So can someone explain to me what this script does line-by-line
It's pretty self-explanatory. Read the documentation on the functions, learn a little bit about sockets, and TRY IT. You'll never know if it'll work if you don't try it.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

JellyFish wrote:Does this mean that I don't need a port open for my server?
In my understanding (as network admin) this mean: Do I need to call my hosting support and inform them that you want to make some outgoing connections so they have to change the server firewall settings to allow you. In short:

to open a port === to make a port not filtered by the firewall
JellyFish wrote: I don't mean to argue with you but it's a bit confusing. Didn't you say "Generally - no, you don't need." in response to my post prior? And doesn't "Generally - no, you don't need." mean I don't need to open a port, but now you say I do? Or is it just a misunderstanding? You thought that the port of mine is already open and I don't need to open it so you said "Generally - no, you don't need.", rather then what I really meant which is that my port isn't open and I'm not authorized to open it. :?
The second part of my post you are quotting was an explanation for what I'd written above:

".... Most of the hosting providers would refuse new connections *TO* their servers (except their service ports), but would allow outgoing connection and the incoming packets which are realated to this connection ..."
JellyFish wrote: And if all posts to a server need to open a port in order to interact ...
Exactly!
JellyFish wrote: ... than who's port is being opened when ajax or an html form makes a request?
While you are browsing in you favorite browser type in the command prompt/console:

netstat -n (windows)
or
netstat -ntp (Linux)
JellyFish wrote: Is it the clients ports that are both connecting? And more importantly, which port umber is being used, to be specific? The usual port 80 is fine, it's just port number 1129 that I'm having trouble with.
If you are sending HTTP request, by default the destination port is 80, and the source port (that's the port which gets opened in your understanding) is usually above 1024 and it must be a free port (i.e. not used by any other process at the local machine). So, you cannot say that it would always be 1129.
JellyFish wrote:
Also is a socket just a connection between two ports? :D
SOCKET
(1) In UNIX and some other operating systems, a software object that connects an application to a network protocol. In UNIX, for example, a program can send and receive TCP/IP messages by opening a socket and reading and writing data to and from the socket. This simplifies program development because the programmer need only worry about manipulating the socket and can rely on the operating system to actually transport messages across the network correctly. Note that a socket in this sense is completely soft - it's a software object, not a physical component.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

VladSun wrote:
JellyFish wrote:Does this mean that I don't need a port open for my server?
SOCKET
(1) In UNIX and some other operating systems, a software object that connects an application to a network protocol. In UNIX, for example, a program can send and receive TCP/IP messages by opening a socket and reading and writing data to and from the socket. This simplifies program development because the programmer need only worry about manipulating the socket and can rely on the operating system to actually transport messages across the network correctly. Note that a socket in this sense is completely soft - it's a software object, not a physical component.
Woaw!!! Wowy wow wow wowww! This clears sooooooo many things UP! Where in the world did you find that description?!?!

It makes so much sense now. A socket is like a file, that you can write to. And this allows you to communicate with another machine on a network! Also, at the end of the description it clarifies that a socket is soft, completely, it's just amazing! Really, I'm not kidding. I know that I asked myself at least once"Is a socket a wire of some kind?" but no, it ain't.

The epiphany that I get from that description also makes me understand what fsockopen does too! It's like a file function that makes a file on the remote server(the machine other then the requester) and returns a resource pointer of that file and allows me to execute other filesystem functions on that resource, like write to it, read it and what not. Am I figuring it out, or am I way off?
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

First Script:

Code: Select all

<?php
	function HttpRequest($address, $data)
	{
		$url = parse_url($address);
		
		while (list($key, $value) = each($data))
		{
			$dataToPost .= "$key=$value&";
		}
		$dataToPost = rtrim($dataToPost, "&");
		
		$request = "POST ".$url['path']." HTTP/1.0\r\n"
			. "Host: ".$url['host']."\r\n"
			. "Content-Type: application/x-www-form-urlencoded;\r\n"
			. "Content-Length: ".strlen($dataToPost)."\r\n"
			. "\r\n"
			. $dataToPost;
		
		$socket = @fsockopen($url['host']);
		fwrite($socket, $request);
		
		// Response
		$response = '';
		while (!feof($socket)) {
		    $response .= fgets($socket, 1160);
		}
		fclose($socket);
		
		return $response;
	}
?>
Second Script:

Code: Select all

$transaction = HttpRequest("https://www.linkpointcentral.com/lpc/servlet/lppay", $myorder);
Error
Warning: fwrite(): supplied argument is not a valid stream resource in /home/content/t/r/a/tradingtresure/html/lib/http.php on line 20

Warning: feof(): supplied argument is not a valid stream resource in /home/content/**************/html/lib/http.php on line 24

Warning: fgets(): supplied argument is not a valid stream resource in /home/content/**************/html/lib/http.php on line 25

Warning: feof(): supplied argument is not a valid stream resource in /home/content/**************/html/lib/http.php on line 24

Warning: fgets(): supplied argument is not a valid stream resource in /home/content/**************/html/lib/http.php on line 25

Warning: feof(): supplied argument is not a valid stream resource in /home/content/**************/html/lib/http.php on line 24

Warning: fgets(): supplied argument is not a valid stream resource in /home/content/**************/html/lib/http.php on line 25

Warning: feof(): supplied argument is not a valid stream resource in /home/content/**************/html/lib/http.php on line 24

Warning: fgets(): supplied argument is not a valid stream resource in /home/content/**************/html/lib/http.php on line 25

Warning: feof(): supplied argument is not a valid stream resource in /home/content/**************/html/lib/http.php on line 24

Warning: fgets(): supplied argument is not a valid stream resource in /home/content/**************/html/lib/http.php on line 25

Warning: feof(): supplied argument is not a valid stream resource in /home/content/**************/html/lib/http.php on line 24

Warning: fgets(): supplied argument is not a valid stream resource in /home/content/**************/html/lib/http.php on line 25

Warning: feof(): supplied argument is not a valid stream resource in /home/content/**************/html/lib/http.php on line 24

Warning: fgets(): supplied argument is not a valid stream resource in /home/content/**************/html/lib/http.php on line 25

ETC...
What am I missing? An argument for fsockopen? All the other parameters other then host where optional.
Last edited by JellyFish on Sat Aug 18, 2007 11:30 am, edited 3 times in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Take error suppression of of fsockopen and see what you get.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

superdezign wrote:Take error suppression of of fsockopen and see what you get.
Same thing... :?

EDIT:

I changed the line to:

Code: Select all

$socket = fsockopen($url['host']) or die("Error: unable to open a socket connection.");
And I get the death.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Code: Select all

$socket = @fsockopen($url['host']) or die('Cannot open the host '.$url['host']);
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

JellyFish wrote: changed the line to:

Code: Select all

$socket = fsockopen($url['host']) or die("Error: unable to open a socket connection.");
And I get the death.
PHP Manual wrote:fsockopen ( string $hostname [, int $port [, int &$errno [, string &$errstr [, float $timeout]]]] )
Make use of &$errno and &$errstr to see the actual failure.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Changed it to spades and got:
Error: unable to open a socket connection with http://www.linkpointcentral.com
Spades:

Code: Select all

$socket = @fsockopen($url['host']) or die('Error: unable to open a socket connection with '.$url['host']);
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

So you can't connect. Now add in the die( ............ 'The error is: '.$errstr);

Edit:Especially for linkpoint i will prefer to use cURL.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

miro_igov wrote:So you can't connect. Now add in the die( ............ 'The error is: '.$errstr);

Edit:Especially for linkpoint i will prefer to use cURL.
... Wait, you use linkpoint? Okay, well they have an API, as you might know, that allows you to use cURL methods and what-not. But I wasn't able to use the API because of my server.

But anyways, I don't understand what you mean by this: die( ............ 'The error is: '.$errstr);. I didn't get that.
Post Reply