Page 1 of 3

Http Posts

Posted: Wed Aug 15, 2007 8:03 pm
by JellyFish
How do I make HTTP post to a remote location with php as you can do with javascript, well javascript can't do it to remote locations but I mean that it can do post to local?

So I repeat my question: How do I make HTTP post with php? :D

Thanks for your time and using it to read my post.

Posted: Wed Aug 15, 2007 8:05 pm
by SidewinderX
You can use CURL

Posted: Wed Aug 15, 2007 8:22 pm
by JellyFish
SidewinderX wrote:You can use CURL
Which function in particular? I could find a simple post function in the cURL function list on php.net.

Posted: Wed Aug 15, 2007 8:26 pm
by superdezign
Sockets can do it.

Posted: Wed Aug 15, 2007 8:27 pm
by superdezign
JellyFish wrote:Which function in particular? I could find a simple post function in the cURL function list on php.net.
You have to do it manually. Google 'POST HTTP/1.0' or something of that nature.

Posted: Wed Aug 15, 2007 8:31 pm
by JellyFish
What about the HTTP functions? http://us2.php.net/manual/en/ref.http.php

I'm a bit confused on how to install them though...

Posted: Wed Aug 15, 2007 8:41 pm
by superdezign
What about them? Are you too lazy to actually learn how to send a post...?

Posted: Wed Aug 15, 2007 8:45 pm
by JellyFish
superdezign wrote:What about them? Are you too lazy to actually learn how to send a post...?
Ehm... I don't know what you mean? I don't understand why php wouldn't have built-in functions to send get and post requests, like ajax. Maybe I don't understand post in the right way.

And btw I have sent a post, just not with php.

Posted: Wed Aug 15, 2007 8:58 pm
by superdezign
JellyFish wrote:Ehm... I don't know what you mean? I don't understand why php wouldn't have built-in functions to send get and post requests, like ajax. Maybe I don't understand post in the right way.

And btw I have sent a post, just not with php.
Ahh. Yeah, sending posts manually actually requires you to write the request, and send it.

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);
That's not tested. I just wrote it...

Posted: Wed Aug 15, 2007 10:07 pm
by JellyFish
superdezign wrote:
JellyFish wrote:Ehm... I don't know what you mean? I don't understand why php wouldn't have built-in functions to send get and post requests, like ajax. Maybe I don't understand post in the right way.

And btw I have sent a post, just not with php.
Ahh. Yeah, sending posts manually actually requires you to write the request, and send it.

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);
That's not tested. I just wrote it...
I think I'm understanding more now. I don't want to make a direct connection with a server as I would do with cURL. You see, this is actually the reason I would like to post information to another location on a remote server.

Here's the situation. My hosting service, a shared service, wouldn't open a port for me. So my only alternative is to post information to a server rather then connect directly. They say to do this with an HTML form. So say I to myself, (what a wonderful world, just kidding), I say to myself "HTML forms are no more then post or get requests to a server, which I could do with ajax, why couldn't I do it with php?". And so I went to php.net an started searching for something and went to the kickass forum and started a thread.

So I don't want to connect to the server(that I'm trying to communicate with) in anyway other then sending a request. I noticed that the fsockopen method acquires a port number in one of there parameters, this makes me think that it's doing exactly what cURL would do.

Posted: Wed Aug 15, 2007 10:25 pm
by VladSun
JellyFish wrote:So I don't want to connect to the server(that I'm trying to communicate with) in anyway other then sending a request. I noticed that the fsockopen method acquires a port number in one of there parameters, this makes me think that it's doing exactly what cURL would do.
It is the remote port, not the local one ...

Posted: Wed Aug 15, 2007 10:33 pm
by JellyFish
VladSun wrote:
JellyFish wrote:So I don't want to connect to the server(that I'm trying to communicate with) in anyway other then sending a request. I noticed that the fsockopen method acquires a port number in one of there parameters, this makes me think that it's doing exactly what cURL would do.
It is the remote port, not the local one ...
Does this mean that I don't need a port open for my server?

Posted: Wed Aug 15, 2007 10:38 pm
by VladSun
Generally - no, you don't need.

Posted: Wed Aug 15, 2007 11:29 pm
by JellyFish
I don't understand php.net's description on fsockopen. What does "Open Internet or Unix domain socket connection" mean? I searched wikipedia for Internet Sockets and Unix Domain Sockets and found two articles about them that, I'm sorry to admit, I dont understand aswell.

How do I find out all this stuff in more simple terms? I find that when I'm reading a description about something it usually includes words that are really specific, but I don't understand the concepts that they refer to. Take this definition for example:

"An Internet socket (or commonly, a socket or network socket), is a communication end-point unique to a machine communicating on an Internet Protocol-based network, such as the Internet."

Yeah that's a great description and all but what I get out of it is a bunch of words, I need visuals. :roll:

Posted: Thu Aug 16, 2007 4:02 am
by miro_igov
cURL is much much better.

Code: Select all

$ch = curl_init('http://example.com/posthandler.php');
 curl_setopt($ch, CURLOPT_POST      ,1);
 curl_setopt($ch, CURLOPT_POSTFIELDS    ,'username='.urlencode($username).'&password='.urlencode($password));
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1); 
 curl_setopt($ch, CURLOPT_HEADER      ,0);  // DO NOT RETURN HTTP HEADERS 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);  // RETURN THE CONTENTS OF THE CALL
 $Rec_Data = curl_exec($ch);
curl_close($ch);