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

User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Http Posts

Post 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.
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

You can use CURL
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Sockets can do it.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post 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...
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

What about them? Are you too lazy to actually learn how to send a post...?
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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...
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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 ...
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: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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Generally - no, you don't need.
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 »

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:
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post 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);
Post Reply