PHP - Sending a packet

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
User avatar
genetix
Forum Contributor
Posts: 115
Joined: Fri Aug 01, 2003 7:40 pm
Location: Sask, Regina
Contact:

PHP - Sending a packet

Post by genetix »

Is there a way to send a PHP packet from one server(php code) to another website?

I need to send a message to a server and get a response using a packet. Is it possible in PHP?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

jadams
Forum Newbie
Posts: 5
Joined: Tue Jun 28, 2005 5:53 pm

Post by jadams »

for more info, check this out: http://us4.php.net/manual/en/ref.curl.php
User avatar
genetix
Forum Contributor
Posts: 115
Joined: Fri Aug 01, 2003 7:40 pm
Location: Sask, Regina
Contact:

Post by genetix »

whats an example of how I could send a packet to a server ip and get a response on a different server.

Like:

send_packet(22.22.22.22, 44.44.44.44, testresponse);
22.22.22.22 = server to send to
44... = server to receive to
User avatar
genetix
Forum Contributor
Posts: 115
Joined: Fri Aug 01, 2003 7:40 pm
Location: Sask, Regina
Contact:

Post by genetix »

Any idea?

I'm not sure which part of curl to use. I have never had to use it before.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

a sample of how I use cURL this is:

Code: Select all

$fields = "field_1=value1&field_2=value2";
$ref = "https://www.yourdomain.com";
$agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"; 
$ch=curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://www.yourserver.com/your_page.php"); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_NOPROGRESS, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,0); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); 
curl_setopt($ch, CURLOPT_TIMEOUT, 120); 
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
curl_setopt($ch, CURLOPT_REFERER, $ref); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$buffer = curl_exec($ch); 
curl_close($ch); 
// build an array of what is returned from the server...burrito
$return = explode(",",$buffer);
obviously, change that you will need to suit your needs. read cURL I suggest you do.
User avatar
genetix
Forum Contributor
Posts: 115
Joined: Fri Aug 01, 2003 7:40 pm
Location: Sask, Regina
Contact:

Post by genetix »

What about using server();

Would that work aswell? I would prefer not having to install extra components. The php.net website says that you have to install the curl module.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

People are suggesting cURL because it's be far the best way to do HTTP comms in PHP. If you're happy doing things at a lower level with raw sockets, use fsockepen() ( http://uk.php.net/fsockopen ) to open a connection, the fwrite() to write something to it.

I doubt there's a way to specify the return IP address. It'd be rather a massive security issue.
Post Reply