The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

fsocketopen() unable to connect

Hi Guys,

I am getting this error on one of my pages where I am trying to connect to Paypal:

PHP Code:

Warning: fsockopen() [function.fsockopen]: unable to connect to http://www.paypal.com/cgi-bin/webscr:80 (Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?) in C:\wamp\www\Software\v2.5\classes\paypal.class.php on line 58

fsockopen() implements sockets at the transport layer. This includes TCP and UDP. HTTP is at the application layer, so fsockopen() doesn't understand what "http://" is. It will, however, understand "tcp://" and "udp://," although it defaults to TCP.

Okay, I am now getting the following error after fixing the "http" problem:

Code:

Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\wamp\www\software\v2.5\classes\paypal.class.php on line 58
Warning: fsockopen() [function.fsockopen]: unable to connect to www.paypal.com/cgi-bin/webscr:80 (php_network_getaddresses: getaddrinfo failed: No such host is known. ) in C:\wamp\www\software\v2.5\classes\paypal.class.php on line 58

You can also use cURL, but I've never used cURL and so I can't help you. There are also other ways to do it, but the ones I posted (namely the latter one) are the simplest and most portable.

Thank you very much for your reply, but I would prefer it if I was able to get fsockopen() working as I am trying to integrate a Paypal IPN script that I have downloaded directly from the Paypal website.

Do you have any idea what may be causing the error? Is there a setting in php.ini that could be causing this error?

The first code I wrote uses fsockopen. You need all that code to implement the bare minimum of the HTTP protocol.

HTTP is at the "application" layer.
TCP is at the "transport" layer.
IP is at the "network" layer.
Below that is the "link" layer. (Where Ethernet, ATM, frame relay, etc. resides.)

A HTTP packet "fits" into a TCP packet. A TCP packet "fits" into an IP packet. Etcetera...

The IP layer contains the IP address of the computer you want to talk to. When you put "www.paypal.com," it's at the IP level of communication. At the TCP layer, you specify the port. In this case, it's port 80. Then, at the HTTP level, you request the URI ("/cgi-bin/webscr" in this case).

fsockopen() speaks at the transport layer. It can talk in TCP and IP, but it doesn't handle HTTP. My first snippet of code in my post implemented the minimum necessary to talk HTTP.

If you want to avoid having to handle HTTP manually, you have to use a library OR make use of PHP's streams/protocol wrappers. My latter code snippet used PHP's streams/protocol wrappers.

----

Edit:

Here's the example from PayPal. Since it uses fsockopen(), it has to implement the bare minimum of the HTTP protocol.

PHP Code:

// read the post from PayPal system and add 'cmd'$req = 'cmd=_notify-validate';